如何使用json.Converter反序列化json



我有一个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);

我认为您的数据不能用合理的类型建模。您需要使用字典和动态对象。我设法反序列化了。

数据:

{
    "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。

您可以使用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);

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

相关内容

  • 没有找到相关文章

最新更新