2016-08-10 6 views
3

Ich versuche, einen benutzerdefinierten singlearrayconverter zu verwenden, um auf die Werte meines json-Arrays zuzugreifen, aber ich bekomme Nullwerte, ich frage mich, wo ich etwas falsch machen könnte.SingleValueArrayConverter eines Arrays, das Null-Eigenschaften zurückgibt

Ich habe folgende JSON:

{ 
    "message": "success", 
    "action": "user_info", 
    "data": { 
    "profile_info": { 
     "querying": "0", 
     "tps": { 
     "1": { 
      "profile_url": "anotherfakeurl", 
      "file_hash": "hash", 
      "icon_path": "", 
      "time_of_insertion": "0000-00-00 00:00:00", 
      "tp_id": "1", 
      "user_id": "4", 
      "tp_user_id": "1377839182243200", 
      "last_use": "0000-00-00 00:00:00", 
      "active": "0", 
      "user_display": "it's a me", 
      "selected": "1", 
      "prof_pic": "fakeurl" 
     } 
     } 
    } 
    } 
} 

Und ich habe folgendes Datenmodell:

[JsonProperty("tps")] 
[JsonConverter(typeof(SingleValueArrayConverter<Tps>))] 
public List<Tps> TpsList { get; set; } 

Und ich bin mit diesem Konverter:

public class SingleValueArrayConverter<T> : JsonConverter 
    { 
     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
     { 
      throw new NotImplementedException(); 
     } 



public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     object retVal = new Object(); 
     if (reader.TokenType == JsonToken.StartObject) 
     { 
      T instance = (T)serializer.Deserialize(reader, typeof(T)); 
      retVal = new List<T>() { instance }; 
     } 
     else if (reader.TokenType == JsonToken.StartArray) 
     { 
      retVal = serializer.Deserialize(reader, objectType); 
     } 
     return retVal; 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return false; 
    } 
} 

und jeden anderen Wert ist richtig, es gibt mir sogar die richtige Array-Anzahl. Der Inhalt jeder Tps-Eigenschaft ist jedoch entweder leer oder null.

Danke,

edit: Hier wird das vollständige Datenmodell ist.

[JsonObject(MemberSerialization = MemberSerialization.OptIn)] 
    public class UserInfo 
    { 
     [JsonProperty("message")] 
     public string Message { get; set; } 

     [JsonProperty("action")] 
     public string Action { get; set; } 

     [JsonProperty("data")] 
     public Data Data { get; set; } 
} 


public class Data 
    { 

     [JsonProperty("profile_info")] 
     public ProfileInformation ProfileInformation { get; set; } 
    } 

public class ProfileInformation 
    { 
    [JsonProperty("querying")] 
     public string Querying { get; set; } 

    [JsonProperty("tps")] 
     [JsonConverter(typeof(SingleValueArrayConverter<Tps>))] 
     public List<Tps> TpsList { get; set; } 
    } 

public class Tps 
    { 
    [JsonProperty("profile_url")] 
     public string ProfileUrl { get; set; } 

     [JsonProperty("file_hash")] 
     public string FileHash { get; set; } 

     [JsonProperty("icon_path")] 
     public string IconPath { get; set; } 

     [JsonIgnore] 
     [JsonProperty("time_of_insertion")] 
     public DateTime TimeOfInsertion { get; set; } 

     [JsonProperty("tp_id")] 
     public int TpId { get; set; } 

     [JsonProperty("user_id")] 
     public int UserId { get; set; } 

     [JsonProperty("tp_user_id")] 
     public long TpUserId { get; set; } 

     [JsonIgnore] 
     [JsonProperty("last_use")] 
     public DateTime LastUse { get; set; } 

     [JsonProperty("active")] 
     public string Active { get; set; } 

     [JsonProperty("user_display")] 
     public string UserDisplay { get; set; } 

     [JsonProperty("selected")] 
     public string Selected { get; set; } 

     [JsonProperty("prof_pic")] 
     public string ProfilePicture { get; set; } 
    } 
+0

Teilen Tps Quelle. CanConvert gibt immer false zurück –

Antwort

0

Ich sage nicht, dass dies der eleganteste oder beste Weg ist, aber es funktioniert. Sie können ein Wörterbuch verwenden, um das gewünschte Verhalten zu erreichen. Also, verlieren Sie den Konverter, dann bewegen Sie hin und her zwischen einer Liste und einem Wörterbuch, der Schlüssel ist, dass TpId Feld scheint es.

public class ProfileInformation 
{ 
    [JsonProperty("querying")] 
    public string Querying { get; set; } 

    List<Tps> _tpsList = null; 
    [JsonIgnore]   
    public List<Tps> TpsList { 
     get { 
      if (_tpsList == null && _tpsDict != null) { 
       _tpsList = _tpsDict.Values.ToList(); 
      }      
      return _tpsList; 
     } 
     set { _tpsList = value; } 
    } 

    Dictionary<int, Tps> _tpsDict = null; 
    [JsonProperty("tps")] 
    public Dictionary<int, Tps> TpsDict { 
     get { 
      if (_tpsDict == null && _tpsList != null) { 
       _tpsDict = _tpsList.ToDictionary(x => x.TpId); 
      } 
      return _tpsDict; 
     } 
     set { _tpsDict = value; } 
    } 
} 

-Test

var userInfo = new UserInfo() { 
      Action = "user_info", 
      Message = "success", 
      Data = new Data() { 
       ProfileInformation = new ProfileInformation() { 
        Querying = "0", 
        TpsList = new List<Tps>() { 
         new Tps() { 
          Active="0", 
          FileHash = "hash", 
          IconPath="", 
          LastUse= DateTime.MinValue, 
          ProfileUrl = "anotherfakeurl", 
          ProfilePicture = "fakeurl", 
          Selected = "1", 
          TimeOfInsertion = DateTime.MinValue, 
          TpId = 1, 
          TpUserId = 1377839182243200L, 
          UserDisplay = "it's a me", 
          UserId = 4 
         } } } } }; 

    string json = JsonConvert.SerializeObject(userInfo, Formatting.Indented); 
    var newUserInfo = JsonConvert.DeserializeObject<UserInfo> (json); 
    Assert.AreEqual(newUserInfo.Data.ProfileInformation.TpsList.Count,1); 
    Assert.AreEqual(newUserInfo.Data.ProfileInformation.TpsDict.Count,1); 
+0

Danke so mcuh! Das hat wie ein Zauber funktioniert! – Mike

Verwandte Themen