通过JSON c#迭代

时间:2016-12-13 17:25:54

标签: c# asp.net-mvc asp.net-mvc-4

我想迭代以下json并且只需从json中取出名称。谁能帮忙。我无法创建任何类,因为在其他类中已存在相同的名称属性。

{   "response": {
     "status": {
       "success": "true",
       "code": 200,
       "message": "SUCCESS"
     },
     "organization": {
       "custom_fields": [
         {
           "field": [
             {
               "name": "age_group",
               "label": "Age Group",
               "type": "text",
               "datatype": "String",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "birthday",
               "label": "Birthday",
               "type": "datepicker",
               "datatype": "String",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "comebackfor",
               "label": "comebackfor",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "customer_feedback",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "countries",
               "label": "countries",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "gender",
               "label": "Gender",
               "type": "select",
               "datatype": "String",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "{\"Male\":\"Male\",\"Female\":\"Female\"}",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "houseblockno",
               "label": "houseblockno",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "interest",
               "label": "interest",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "likeproducts",
               "label": "likeproducts",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "customer_feedback",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "maritalstatus",
               "label": "maritalstatus",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "nationality",
               "label": "nationality",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "occupation",
               "label": "occupation",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "pincode",
               "label": "Pincode",
               "type": "text",
               "datatype": "String",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "rateus",
               "label": "rateus",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "customer_feedback",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "salutation",
               "label": "salutation",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "service",
               "label": "service",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "customer_feedback",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "streetname",
               "label": "streetname",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "suggestions",
               "label": "suggestions",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "customer_feedback",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             },
             {
               "name": "unitno",
               "label": "unitno",
               "type": "text",
               "datatype": "Boolean",
               "default": "",
               "phase": "1",
               "position": "0",
               "rule": "",
               "regex": "",
               "error": "",
               "options": "",
               "scope": "loyalty_registration",
               "is_mandatory": "0",
               "is_updatable": "1",
               "is_disabled": "0",
               "disabled_at_server": "0"
             }
           ]
         }
       ]
     }   
   } 
}

1 个答案:

答案 0 :(得分:-2)

使用Json.Net之类的内容:

var anon = JsonConvert.DeserializeObject(<YourStringHere>);
foreach(var item in anon.organisation.custom_fields)
{
    Console.WriteLine(item.Name)
}

这将反序列化为匿名对象,并允许您像任何其他类允许的那样迭代内容。您也可以使用linq select替换foreach。

anon.organisation.custom_fields.Select(x => x.Name);

道歉,如果这不是完全正确的,没有实际反序列化json,我不能100%确定它将创建什么样的结构。