如何用Json.Converter反序列化json

时间:2013-12-03 13:35:20

标签: c# json windows-phone-8 json.net deserialization

我有一个json:

 "objects": {
                    "user0": {
                        "uid": "1",
                    }
                    "user1": {
                        "uid": "2",
                    }
                    "user2": {
                        "uid": "3",
                    }

                    .....

                    "userN": {
                        "uid": "N",
                    }

                    "venue0": {
                        "id": "1",
                    }
                    "venue1": {
                        "id": "2",
                    }
                    "venue2": {
                        "id": "3",
                    }

                    .....

                    "venueN": {
                        "id": "N",
                    }
             }

我有一个模特:

[DataContract]
public class User
{
    [DataMember(Name = "uid")]
    public class string Uid {get; set;}
}

[DataContract]
public class Venue
{
    [DataMember(Name = "id")]
    public class string Id {get; set;}
}


[DataContract]
public class Objects
{
    [DataMember(Name = "user1")]
    public class User User1 {get; set;}

    [DataMember(Name = "user2")]
    public class User User2 {get; set;}

    [DataMember(Name = "venue1")]
    public class Venue Venue1 {get; set;}

    [DataMember(Name = "venue2")]
    public class Venue Venue2 {get; set;}


    //I can't define all keys of json
    // If the json contain 10000 users or venues, I must declare 10000 properties in class Objects? 
}

我可以使用JsonConverter

反序列化json
Objects objs = JsonConvert.DeserializeObject<Objects>(json);

2 个答案:

答案 0 :(得分:2)

我认为您的数据无法使用合理的类型建模。您需要使用词典和动态对象。我设法以某种方式反序列化。

数据:

{
    "objects": {
        "user0": {
            "uid": "1",
        },
        "user1": {
            "uid": "2",
        },
        "user2": {
            "uid": "3",
        },
        "userN": {
            "uid": "N",
        },
        "venue0": {
            "id": "1",
        },
        "venue1": {
            "id": "2",
        },
        "venue2": {
            "id": "3",
        },
        "venueN": {
            "id": "N",
        }
    }
}

代码:

public class RootObject
{
    public Dictionary<string, dynamic> Objects { get; set; }
}

RootObject deserializedObject = JsonConvert.DeserializeObject<RootObject>(data);

如果您需要创建UserVenue个实例,可以执行以下操作:

List<User> users = new List<User>();
List<Venue> venues = new List<Venue>();

foreach (string key in deserializedObject.Objects.Keys)
{
    if (key.StartsWith("user", StringComparison.InvariantCultureIgnoreCase))
    {
        users.Add(new User { Uid = deserializedObject.Objects[key].uid });
    }

    if (key.StartsWith("venue", StringComparison.InvariantCultureIgnoreCase))
    {
        venues.Add(new Venue { Id = deserializedObject.Objects[key].id });
    }
}

我觉得这很乱,但确实有效。如果可以的话,重构你的JSON。

答案 1 :(得分:1)

您可以使用javascript序列化程序。

导入命名空间

using System.Web.Script.Serialization;

并使用下面的代码反序列化您的json字符串:

JavaScriptSerializer sr = new JavaScriptSerializer();
string jsondata = /*your json string here*/;
//Now with this line the Json string will be converted in MyObj1 object type
MyObj1 converted = sr.Deserialize<Object>(jsondata);

有关详细信息,请参阅This链接。