2017-09-13 104 views
0

我有這樣一個jsonString:嵌套Json-如何訪問項目?

{ 
"total": 0, 
"subtotal": 88, 
"page": 1, 
"per_page": 100, 
"search": "records", 
"sort": { 
    "by": null, 
    "order": null 
}, 
"results": { 
    "x-name": { 
     "records": "[{\"instance\":\"devsupport\",\"details\":[{\"id\":\"2\",\"hostname\":\"a\",\"ip\":\"i\",\"macaddr\":\"m\",\"user_created\":\"system\",\"date_created\":\"2015-07-10 11:45:20\",\"date_last_update\":null}]" 
    }, 
    "y-name": { 
     "records": "[{\"instance\":\"devsupport\",\"details\":[{\"id\":\"2\",\"hostname\":\"a\",\"ip\":\"i\",\"macaddr\":\"m\",\"user_created\":\"system\",\"date_created\":\"2015-07-10 11:45:20\",\"date_last_update\":null}]" 
    } 

    } 
} 

所以我jo object創建這樣的正確diplays:

Object jo = JObject.Parse(jsonString); 

我的主要問題是如何能得到hostname,ip,macadd R和等,並把他們在列表中考慮到 「結果」中的記錄具有所有不同的名稱,如x-name, y-name等。

+1

你將不得不在'x-name'或'y-name'中解析'records'的值,因爲它是一個JSON字符串。 –

回答

0

您必須更改Json或解析裏面records再次JSON,有像object->results->xname->records->ip

沒有其他的直接的方法你可以不喜歡

recordsjson = object->results->xname->records 
records = JObject.Parse(recordsjson); 
ip = records->ip 

或者

改變你的records結構這樣一個

"records": [ 
    { 
     "instance": "devsupport", 
     "details": [ 
     { 
      "id": "2", 
      "hostname": "a", 
      "ip": "i", 
      "macaddr": "m", 
      "user_created": "system", 
      "date_created": "2015-07-10 11:45:20", 
      "date_last_update": null 
     } 
     ] 
    } 
    ]