所以我需要從我的C#項目解析JSON字符串。 讓我的JSON從一個方法調用的響應和JSON看起來是這樣的:C#通過ID訪問JSON內容
{
"10": {
"entity_id": "10",
"attribute_set_id": "4",
"type_id": "simple",
"sku": "convertor-touchscreen",
"name": "Convertor touchscreen",
"meta_title": null,
"meta_description": null,
"url_key": "convertor-touchscreen",
"custom_design": null,
"page_layout": null,
"options_container": "container1",
"country_of_manufacture": null,
"msrp_enabled": "2",
"msrp_display_actual_price_type": "4",
"gift_message_available": null,
"creareseo_discontinued": null,
"creareseo_discontinued_product": null,
"description": "Convertor touchscreen",
"short_description": "Convertor touchscreen",
"meta_keyword": null,
"custom_layout_update": null,
"price": "421.0000",
"special_price": "380.0000",
"weight": "0.1500",
"msrp": null,
"special_from_date": "2015-11-24 00:00:00",
"special_to_date": "2015-11-26 00:00:00",
"news_from_date": null,
"news_to_date": null,
"custom_design_from": null,
"custom_design_to": null,
"status": "1",
"visibility": "4",
"tax_class_id": "2",
"featured": "1"
}
}
所以我需要訪問的成員,如「ENTITY_ID」,「姓名」等...... 所以我嘗試
using Newtonsoft.Json.Linq;
...
// output is the above JSON string
var jo = JObject.Parse(output);
var id = jo[0]["entity_id"].ToString();
但顯然它不是做一個好辦法。 另外,我沒有控制權的第一部分
{
"10": {
所以我不能做
var id = jo["10"]["entity_id"].ToString();
,因爲我不知道該值「10」將在我的下一個JSON字符串的內容。 那麼如何通過Id或其他東西獲取元素值?
你不知道你*知道什麼。在JSON中只會有一個實體嗎?你認爲什麼是對你所擁有的東西「不是一個好方法」? –