2016-12-13 9 views
-4

Ich möchte durch den folgenden JSON durchlaufen und muss nur Namen aus dem JSON herausnehmen. Kann jemand helfen. Ich kann keine Klasse erstellen, da in einer anderen Klasse bereits dieselben Eigenschaften vorhanden sind.Iterieren durch 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

Es ist nicht wirklich klar, was du meinst. Warum kann man das nicht einfach in eine Klasse deserialisieren und die gewünschten Werte erhalten? Der Deserializer von Newtonsoft macht das ziemlich trivial. – David

+0

Gehen Sie hier und fügen Sie Ihre JSON hier, um es zu Ihrem Kurs zu generieren oder Sie können dies direkt aus Visual Studio sowie http://json2csharp.com/ – MethodMan

+0

json.net ist Ihr Freund – pm100

Antwort

-2

Mit Json.Net seine so etwas wie:

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

Dies zu einem anonymen Objekt deserialise wird, und lassen Sie die Inhalte als jede andere Klasse, die Sie erlauben würde, zu durchlaufen. Sie können die Foreach auch durch eine Linq-Auswahl ersetzen.

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

Entschuldigt, wenn dies nicht ganz richtig ist, ohne tatsächlich, dass json deserialising ich nicht 100% sicher sein kann, welche Struktur es schaffen wird.

+0

Dies wird ein JObject erstellen, aber die 'anon'-Variable ist immer noch definiert als' Objekt' - so oder so Sie können nicht direkt auf die Eigenschaften zugreifen. Sie können 'dynamic' verwenden oder einen starken Typ erstellen. –