Retrieve nested jsonobjects

有些话、适合烂在心里 提交于 2019-12-12 00:15:56

问题


I am using Unity C# and want to retrieve data from a nested json object sent from my push notification.

The json object looks like this:

{
    "aps":
    {
        "badge":1
       ,"sound":"default.mp3"
       ,"alert":"test my push"
    }
    ,"u":"{\"custom\":\"{\"gameid\":\"12345\"}\"}
}

I the retrieve the json like this:

string payload = aboveJsonString;
Hashtable table = (Hashtable)easy.JSON.JsonDecode(payload);
Hashtable data = (Hashtable)easy.JSON.JsonDecode(table["u"].ToString());
Hashtable gamejson =(Hashtable)easy.JSON.JsonDecode(data["custom"].ToString());

Everything is right until the gamejson?

data["custom"] prints {"gameid":"12345"} 
gamejson["gameid"] prints nothing?!?

I am kind a lost in this. Can anyone please help me with this and any help is appreciated.

Thanks in advance :-)


回答1:


You can Use Simple Json as it is best for Unity scripting. and regarding your problem it is escape sequences in your json string. If you are generating this json on your own server. you should avoid it. Otherwise you need to remove escape sequesnces from string before parsing it.



来源:https://stackoverflow.com/questions/31936313/retrieve-nested-jsonobjects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!