2016-12-13 146 views
-4

我想遍歷下面的json,只需要從json中取出名稱。任何人都可以幫忙我無法創建任何類,因爲在其他類中已經有相同的名稱屬性。通過JSON迭代c#

{ "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" 
      } 
      ] 
     } 
     ] 
    } 
    } 
} 
+0

這不太清楚你的意思。爲什麼你不能簡單地將它反序列化爲一個類,並從中獲得你想要的值? Newtonsoft的反序列化器使得這個工作非常簡單。 – David

+0

去這裏粘貼你的json在這裏讓它生成你的json到一個類,或者你可以直接從Visual Studio中做到這一點http://json2csharp.com/ – MethodMan

+0

json.net是你的朋友 – pm100

回答

-2

使用Json.Net它是這樣的:

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

這將deserialise一個匿名對象,並允許您遍歷內容作爲其他類將使你。你也可以用linq select替換foreach。

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

道歉,如果這是不完全正確的,實際上並沒有deserialising是JSON我不能100%肯定它會造成什麼樣的結構。

+0

這將創建一個JObject,但是'anon'變量仍然被定義爲'object' - 無論哪種方式,你不能直接訪問這些屬性。你可以使用'dynamic',或者你可以創建一個強類型。 –