遇到Json的key不固定时,只需要解析value,如下Json data下的key(1和2)是变化的:
{
"status": 1,
"msg": "success",
"data": [
"1:": {
"atitle": "test",
"imgurl": "http://test.png",
"cgid": "1",
"saedate": "05/02/2020 - 05/06/2020"
},
"2:": {
"atitle": "test",
"imgurl": "http://test.png",
"cgid": "1",
"saedate": "05/02/2020 - 05/06/2020"
}
]
}
var jsonObject = MiniJSON.jsonDecode(str);
var jsonToHashtable = jsonObject as Hashtable;
if (jsonToHashtable != null) {
if (jsonToHashtable.Contains("status")) {
var statusObj = jsonToHashtable["status"];
int statusInt = Convert.ToInt32(statusObj);
if (statusInt == 1) {
if (jsonToHashtable.Contains("data")) {
var data = jsonToHashtable["data"];
var dataHashtable = data as Hashtable;
foreach(var values in dataHashtable.Values) {
string dataItemStr = MiniJSON.jsonEncode(values);
PayInfo payInfo = JsonConvert.DeserializeObject<PayInfo>(dataItemStr);
}
}
}
}
}
public class PayInfo {
public string atitle = "";
public int cgid;
public string imgurl;
public string saedate;
}
标签:string,jsonToHashtable,Json,test,Unity,key,var,data,public
From: https://blog.51cto.com/u_16289922/9018266