Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#通过ID访问JSON内容_C#_Json - Fatal编程技术网

C#通过ID访问JSON内容

C#通过ID访问JSON内容,c#,json,C#,Json,所以我需要从我的C#项目中解析一个JSON字符串。 我将JSON作为方法调用的响应,JSON如下所示: { "10": { "entity_id": "10", "attribute_set_id": "4", "type_id": "simple", "sku": "convertor-touchscreen", "name": "Convertor touchscreen", "meta_

所以我需要从我的C#项目中解析一个JSON字符串。 我将JSON作为方法调用的响应,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"
    }
}
所以我需要访问成员,如“实体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();
因为我不知道下一个JSON字符串中的值“10”是什么。
那么如何通过Id或其他方式获取元素值呢?

如果您事先不知道
10
的值,很明显您无法使用此键访问其内容(在您的示例中,该键将是访问其他属性的键)。如果此对象始终具有1个顶级属性,则您的代码很好:

var jo = JObject.Parse(output);
var id = jo[0]["entity_id"].ToString();
在尝试访问
jo
数组之前,添加一些防御性检查,确保该数组至少有一个属性


另一方面,如果键位于您知道的实体的某个属性内,则可以随意循环
jo
变量的所有顶级属性,检查您知道的键的值的子属性,直到找到所需的记录。

因此我自己得出了一个结论: 如果JSON只包含一个顶级元素(在我的例子中是“10”),那么我可以访问该元素中的属性,如下所示:

var descr = output.description;
var entid = output.entity_id;
... and so on

我仍然不知道如何访问包含多个顶级1元素的JSON。但我会提出另一个问题。

不清楚你知道什么。JSON中是否只有一个实体?你认为你所拥有的“不是一个好方法”是什么?我认为你没有抓住要点。我无法访问该属性,因为我不知道“10”的值。所以我必须通过ID,但如果我尝试这样做,我会得到一个错误。因此,您在上面编写的代码与我在问题中作为非工作代码示例放置的代码相同:错误:
使用无效键值访问JObject值:0。需要对象属性名。
我找到了一个可行的解决方案,所以我将稍微改变一下这个问题:如果有多个顶级元素,而我不知道它们,我如何访问JSON?如果有多个顶级元素,而你不知道它们,你不知道你想要访问什么,那么你到底希望能够实现什么?当然,您可以循环遍历所有顶级元素,但是如果您不知道您在寻找什么,那么找到它的机会接近于零。