-5

hi i have generated following json , can i iterate through this i have validated this josn using some sites

following is just example there can be more section like MATHS, LOGICAL REASONING ,ENGLISH

they can also have their individual types

 {    "MATHS": [
    {
        "Section": "MATHS",
        "Type": "INCORRECT",
        "Count": "5"
    },
    {
        "Section": "MATHS",
        "Type": "NOT SOLVED",
        "Count": "20"
    }
],
"LOGICAL REASONING": [
    {
        "Section": "LOGICAL REASONING",
        "Type": "CORRECT",
        "Count": "1"
    },
    {
        "Section": "LOGICAL REASONING",
        "Type": "INCORRECT",
        "Count": "4"
    },
    {
        "Section": "LOGICAL REASONING",
        "Type": "NOT SOLVED",
        "Count": "20"
    }
]
 }

i have searched on may question on stack overflow but none of them can help me

Dhiraj Wakchaure
  • 2,596
  • 6
  • 21
  • 37
  • 3
    and probably [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/q/85992/218196) – Felix Kling May 01 '14 at 12:29
  • 2
    and maybe [How to parse JSON in JavaScript](http://stackoverflow.com/q/4935632/218196) – Felix Kling May 01 '14 at 12:29
  • From 1st link i can get only MATHS not count or other things, before criticising just take overview of problem , those questions are diffrent in all manner from this – Dhiraj Wakchaure May 01 '14 at 12:36
  • No, they are not different at all. They explain how to go from JSON to an object (containing arrays) and how to access (nested) objects and arrays. All you have to do is put the pieces together. – Felix Kling May 01 '14 at 12:39
  • @FelixKling if you think yourself smart can make one fiddle for me for this – Dhiraj Wakchaure May 01 '14 at 12:40
  • 1
    Maybe this helps to get started: `for (var prop in data) { for (var i = 0; i < data[prop].length; i++) { console.log(data[prop][i]); } }`. `data` is your object, parsed from JSON. This iterates over all the objects in the array and you can access properties you want. This btw is explained in [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json), section "What if the property names are dynamic and I don't know them beforehand?". – Felix Kling May 01 '14 at 12:42
  • @FelixKling http://jsfiddle.net/A8e3G/ not working – Dhiraj Wakchaure May 01 '14 at 12:47
  • 1
    Works fine for me. Did you have a look at the console? Didn't you see the objects logged? [Learn how to debug JavaScript](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820). If that's not what you want, I'm afraid it's not clear what you want (but it *does* "work"). – Felix Kling May 01 '14 at 12:48
  • found answer i took data in var jsondata and following code works perfect == parseddata =jQuery.parseJSON(jsondata) $.each(parseddata , function (index,entry) { $("#out").append(index + "
    ") $.each (entry , function(ind,ent){ $("#out").append(ent.Type+ + "
    ") }) })
    – Dhiraj Wakchaure May 02 '14 at 13:55
  • @FelixKling i found answer http://jsfiddle.net/KL7r2/ – Dhiraj Wakchaure May 02 '14 at 14:05
  • @PatrickHofman no regret,they dont give answer so i found my answer myself :) – Dhiraj Wakchaure May 02 '14 at 14:10
  • Yep, that's exactly the same as what I wrote, just using jQuery's `$.each` method instead of `for` loops. – Felix Kling May 02 '14 at 14:46
  • @FelixKling but using that for loop it wont give what i exactly need , i had also tried that way too , but wont get expected results – Dhiraj Wakchaure May 03 '14 at 06:22

1 Answers1

0

Your JSON has 2 top-level elements. So, you can't logically iterate across the entire document (without flattening it).

But you can iteration across the "MATHS" and "LOGICAL REASONING" elements.

For example, using underscore:

_(data.MATHS).each(function(item) {
  var section = item.SECTION;
  var type = item.TYPE;
  var count = item.COUNT;

  // Now do something with them
});

Note the different access method to the 'LOGICAL REASONING' element, because of the space.

_(data['LOGICAL REASONING').each(function(item) {
  var section = item.SECTION;
  var type = item.TYPE;
  var count = item.COUNT;

  // Now do something with them
});

If you wanted all the data together, one way of doing it would be:

var flattened = _.union(data.MATHS, data['LOGICAL REASONING']);

_(flattened).each(function(item) {
  // Process...
});

Check out the underscore docs for some more information about the possibilities. Many ways to skin a cat.

http://underscorejs.org/

Cheers.

Johnny Hall
  • 545
  • 4
  • 12