UWP和嵌套的json对象

时间:2017-04-05 14:29:57

标签: c# json uwp

我试图在c#applicatie中反序列化一些JSON代码。我使用DataContractJsonSerializer类来执行此操作。一切正常,直到我得到嵌套的jsonobjects,

我从外部API接收以下json:

[{"name":"Beeldscherm","json":"{\"onHomeEnter\":{\"state\":0,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487166732440},\"onHomeExit\":{\"state\":0,\"delay\":120,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487163669388},\"onRoomEnter\":{\"state\":1,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1},\"onRoomExit\":{\"state\":0,\"delay\":120,\"fadeTime\":0,\"active\":false,\"updatedAt\":1},\"onNear\":{\"state\":1,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487164911729},\"onAway\":{\"state\":0,\"delay\":2,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487164909184}}","id":"58a44fa04f40b016004ffca2","sphereId":"58a44c0a4f40b016004ffc9d","createdAt":"2017-02-15T13:52:12.783Z","updatedAt":"2017-02-15T12:55:05.332Z"}]

我使用以下代码编辑:

responJsonText = responJsonText.Replace("\\", "").Replace("\"{", "{").Replace("}\"", "}");

然后它看起来像这样:

[{"name":"Beeldscherm","json":{"onHomeEnter":{"state":0,"delay":0,"fadeTime":0,"active":false,"updatedAt":1487166732440},"onHomeExit":{"state":0,"delay":120,"fadeTime":0,"active":false,"updatedAt":1487163669388},"onRoomEnter":{"state":1,"delay":0,"fadeTime":0,"active":false,"updatedAt":1},"onRoomExit":{"state":0,"delay":120,"fadeTime":0,"active":false,"updatedAt":1},"onNear":{"state":1,"delay":0,"fadeTime":0,"active":false,"updatedAt":1487164911729},"onAway":{"state":0,"delay":2,"fadeTime":0,"active":false,"updatedAt":1487164909184}},"id":"58a44fa04f40b016004ffca2","sphereId":"58a44c0a4f40b016004ffc9d","createdAt":"2017-02-15T13:52:12.783Z","updatedAt":"2017-02-15T12:55:05.332Z"}]

并尝试使用以下类解析它:

[DataContract]
class Appliance
{
    [DataMember]
    public String name { get; set; }
    [DataMember]
    public ApplianceJson json { get; set; }
    [DataMember]
    public String id { get; set; }
    [DataMember]
    public String sphereId { get; set; }
    [DataMember]
    public String createdAt { get; set; }
    [DataMember]
    public String updatedAt { get; set; }
}

[DataContract]
class ApplianceJson
{
    [DataMember]
    public ApplianceEvents onHomeEnter { get; set; }
    [DataMember]
    public ApplianceEvents onHomeExit { get; set; }
    [DataMember]
    public ApplianceEvents onRoomEnter { get; set; }
    [DataMember]
    public ApplianceEvents onRoomExit { get; set; }
    [DataMember]
    public ApplianceEvents onNear { get; set; }
    [DataMember]
    public ApplianceEvents onAway { get; set; }
}

[DataContract]
class ApplianceEvents
{
    [DataMember]
    public int state { get; set; }
    [DataMember]
    public int delay { get; set; }
    [DataMember]
    public int fadeTime { get; set; }
    [DataMember]
    public bool active { get; set; }
    [DataMember]
    public int updatedAt { get; set; }
}

以及反序列化的以下代码:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Appliance>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(responJsonText));

我得到的错误是:

Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.Private.DataContractSerialization.dll
Exception thrown: 'System.Runtime.Serialization.SerializationException' in mscorlib.ni.dll

有人看到错误的东西以及我如何解决它?

更新 我现在检查了json字符串及其正确,我已将正确的版本放入问题

更新2 添加了原始的JSON

1 个答案:

答案 0 :(得分:0)

如果您注意到原始Json,您可以看到"不是\"但是如果深入json,则Json节点本身"\"。所以我首先清理了所有\",然后再次使用"使用Newtonsoft Json替换\",然后成功解析。看到我的回答。

使用This Site和您的JsonString,下面是我创建的正确的Json类结构。

public class Appliance
{
    public string name { get; set; }
    public Json json { get; set; }
    public string id { get; set; }
    public string sphereId { get; set; }
    public string createdAt { get; set; }
    public string updatedAt { get; set; }
}

public class OnHomeEnter
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public long updatedAt { get; set; }
}

public class OnHomeExit
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public long updatedAt { get; set; }
}

public class OnRoomEnter
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public int updatedAt { get; set; }
}

public class OnRoomExit
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public int updatedAt { get; set; }
}

public class OnNear
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public long updatedAt { get; set; }
}

public class OnAway
{
    public int state { get; set; }
    public int delay { get; set; }
    public int fadeTime { get; set; }
    public bool active { get; set; }
    public long updatedAt { get; set; }
}

public class Json
{
    public OnHomeEnter onHomeEnter { get; set; }
    public OnHomeExit onHomeExit { get; set; }
    public OnRoomEnter onRoomEnter { get; set; }
    public OnRoomExit onRoomExit { get; set; }
    public OnNear onNear { get; set; }
    public OnAway onAway { get; set; }
}

完成后,我使用Json.Net 将JsonString解析为Appliance对象

下面是Final Json String and Parser。

"[{\"name\":\"Beeldscherm\",\"json\":{\"onHomeEnter\":{\"state\":0,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487166732440},\"onHomeExit\":{\"state\":0,\"delay\":120,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487163669388},\"onRoomEnter\":{\"state\":1,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1},\"onRoomExit\":{\"state\":0,\"delay\":120,\"fadeTime\":0,\"active\":false,\"updatedAt\":1},\"onNear\":{\"state\":1,\"delay\":0,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487164911729},\"onAway\":{\"state\":0,\"delay\":2,\"fadeTime\":0,\"active\":false,\"updatedAt\":1487164909184}},\"id\":\"58a44fa04f40b016004ffca2\",\"sphereId\":\"58a44c0a4f40b016004ffc9d\",\"createdAt\":\"2017 - 02 - 15T13: 52:12.783Z\",\"updatedAt\":\"2017 - 02 - 15T12: 55:05.332Z\"}]";

以下是我成功解析Json的方法。

List<Appliance> appJson = JsonConvert.DeserializeObject<List<Appliance>>(JsonData);

祝你好运。