Retrieve nested jsonobjects

2019-09-11 16:03发布

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条回答
smile是对你的礼貌
2楼-- · 2019-09-11 16:50

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.

查看更多
登录 后发表回答