2017-04-05 94 views
0

我試圖反序列化c#applicatie中的一些JSON代碼。我使用DataContractJsonSerializer類來執行此操作。一切工作正常,直到我得到的嵌套一個JSONObjects,UWP和嵌套的json對象

我收到以下JSON從外部API:

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

你的Json不解析。嘗試在線Json解析器,看看你是否收到適當的Json。 – AVK

+0

你應該看看是否有內部異常有更多的細節。 – crashmstr

+0

@AVKNaidu那裏確實有json的錯誤,這是因爲給了JSON的API將字符串中json對象的json元素轉換爲字符串,而我只刪除了\,而不是「圍繞該對象,但它仍然沒有沒有工作。 – furrie

回答

0

如果您發現原始Json,你可以看到"不是\",但是如果你深入JSON,Json節點本身有"作爲\"。所以我首先清理了所有的\",然後再次用"替換\"使用了Newtonsoft Json,然後解析成功。看到我的答案。

使用This站點和您的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對象

下面是最後的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\"}]"; 

以下是我如何成功解析Json。

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

祝你好運。

+1

我發現答案就在你發佈答案之前,只是「(我已經刪除了),而且還是」在json塊周圍,用{} {用{和}}替換它後,它工作了,但是謝謝你研究它。 – furrie