locked
foreach loop on json object

    Question

  • I am having a hard time going through a Jason string. the Json class I obtain from the API and json2c# is the code below. can some show me a for each loop that will iterate through it? I used

    RootObject Ro = sonConvert.DeserializeObject<RootObject>(response)

    for each(var item in Ro){

    }

    I have tried it and it doesn't work.

    public class RootObject
    {
        public string name { get; set; }
        public int id { get; set; }
    }
    json strings
    [{"name":"English","id":33},{"name":"Spanish","id":107},{"name":"German","id":44},{"name":"French","id":39},{"name":"Italian","id":56},{"name":"Portuguese","id":87},{"name":"Russian","id":94},{"name":"Polish","id":86},{"name":"Turkish","id":118},{"name":"Japanese","id":57},{"name":"Chinese - Mandarin","id":26},{"name":"- other -","id":0},{"name":"Afrikaans","id":1},{"name":"Akan","id":2},{"name":"Albanian","id":3},{"name":"Amharic (Ethiopian)","id":4},{"name":"Arabic - Egyptian","id":5},{"name":"Arabic - Middle Eastern","id":6},{"name":"Arabic - Moroccan","id":7},{"name":"Arabic - other","id":8},{"name":"Aramaic","id":9},{"name":"Armenian","id":10},{"name":"Assamese","id":11},{"name":"Assyrian","id":12},{"name":"Azerbaijani","id":13},{"name":"Basque","id":14},{"name":"Belarusian","id":15},{"name":"Bengali","id":16},{"name":"Berber","id":17},{"name":"Bosnian","id":18},{"name":"Breton","id":19},{"name":"Bulgarian","id":20},{"name":"Burmese","id":21},{"name":"Cambodian (Khmer)","id":22},{"name":"Catalan","id":23},{"name":"Chichewa (Nyanja)","id":24},{"name":"Chinese - Cantonese","id":25},{"name":"Chinese - Mandarin","id":26},{"name":"Chinese - other","id":27},{"name":"Creole","id":28},{"name":"Croatian","id":29},{"name":"Czech","id":30},{"name":"Danish","id":31},{"name":"Dutch","id":32},{"name":"English","id":33},{"name":"Esperanto","id":34},{"name":"Estonian","id":35},{"name":"Fijian","id":36},{"name":"Filipino (Tagalog)","id":37},{"name":"Finnish","id":38},{"name":"French","id":39},{"name":"Gaelic (Irish)","id":40},{"name":"Gaelic (Scottish)","id":41},{"name":"Galician","id":42},{"name":"Georgian","id":43},{"name":"German","id":44},{"name":"Greek","id":45},{"name":"Gujarati","id":46},{"name":"Hawaiian","id":47},{"name":"Hebrew","id":48},{"name":"Hindi","id":49},{"name":"Hmong","id":50},{"name":"Hungarian","id":51},{"name":"Icelandic","id":52},{"name":"Ido","id":53},{"name":"Indonesian (Bahasa)","id":54},{"name":"Interlingua","id":55},{"name":"Italian","id":56},{"name":"Japanese","id":57},{"name":"Kannada","id":58},{"name":"Kazakh","id":59},{"name":"Konkani","id":60},{"name":"Korean","id":61},{"name":"Kurdish","id":62},{"name":"Ladino","id":63},{"name":"Lao","id":64},{"name":"Latin","id":65},{"name":"Latvian","id":66},{"name":"Ligurian","id":67},{"name":"Lithuanian","id":68},{"name":"Lombard","id":69},{"name":"Macedonian","id":70},{"name":"Malagasy","id":71},{"name":"Malay (Bahasa Malaysia)","id":72},{"name":"Malayalam","id":73},{"name":"Maldivian (Dhivehi)","id":74},{"name":"Maltese","id":75},{"name":"Maori (New Zealand Maori)","id":76},{"name":"Marathi","id":77},{"name":"Mongolian","id":78},{"name":"Native American","id":79},{"name":"Nepali","id":80},{"name":"Newari","id":81},{"name":"Norwegian","id":82},{"name":"Oriya","id":83},{"name":"Paraguayan Guaraní","id":84},{"name":"Persian (Farsi)","id":85},{"name":"Polish","id":86},{"name":"Portuguese","id":87},{"name":"Pulaar","id":88},{"name":"Punjabi","id":89},{"name":"Pushto (Pashto)","id":90},{"name":"Quechua","id":91},{"name":"Romanian","id":92},{"name":"Romansch","id":93},{"name":"Russian","id":94},{"name":"Saami (sami)","id":95},{"name":"Samoan","id":96},{"name":"Sanskrit","id":97},{"name":"Serbian (Serbo-Croatian)","id":98},{"name":"Shona","id":99},{"name":"Sicilian","id":100},{"name":"Sign Language","id":101},{"name":"Sindhi","id":102},{"name":"Sinhalese","id":103},{"name":"Slovak","id":104},{"name":"Slovenian","id":105},{"name":"Somali","id":106},{"name":"Spanish","id":107},{"name":"Swahili","id":108},{"name":"Swedish","id":109},{"name":"Tamil","id":110},{"name":"Tatar (Tartar)","id":111},{"name":"Telugu","id":112},{"name":"Tetum","id":113},{"name":"Thai","id":114},{"name":"Tibetan","id":115},{"name":"Tongan","id":116},{"name":"Tswana","id":117},{"name":"Turkish","id":118},{"name":"Ukrainian","id":119},{"name":"Urdu","id":120},{"name":"Uyghur (Wighor)","id":121},{"name":"Uzbek","id":122},{"name":"Venetian","id":123},{"name":"Vietnamese","id":124},{"name":"Welsh","id":125},{"name":"Wolof","id":126},{"name":"Xhosa","id":127},{"name":"Yiddish","id":128},{"name":"Yoruba","id":129},{"name":"Zulu","id":130}]

    Monday, February 16, 2015 10:39 PM

Answers

  • Hello Benjimola,

    Since your Json response is a list of items, your return .net object when deserializing would have to be a list of the objects. Here's the code below:

    var RootObjects = JsonConvert.DeserializeObject <List<RootObject>>(jsonValue);
    
                foreach (var rootObject in RootObjects)
                {
                    //do something here
                }

    Hope this helps


    Abdulwahab Suleiman

    Tuesday, February 17, 2015 1:06 AM
    Moderator

All replies

  • Hello Benjimola,

    Since your Json response is a list of items, your return .net object when deserializing would have to be a list of the objects. Here's the code below:

    var RootObjects = JsonConvert.DeserializeObject <List<RootObject>>(jsonValue);
    
                foreach (var rootObject in RootObjects)
                {
                    //do something here
                }

    Hope this helps


    Abdulwahab Suleiman

    Tuesday, February 17, 2015 1:06 AM
    Moderator
  • thanks... I finally figured it out. thanks for the tip.
    Tuesday, February 17, 2015 2:20 AM