2

Here is my JSON data :

{
    "main": [
        {
            "main_id": "1234",
            "main_sub": [
                {
                    "main_info": {
                        "main_1" : "A",
                        "main_2" : "B",
                        "main_3" : "C"
                    },
                    "main_4" : "D",
                    "main_5" : "E",
                },
                {
                    "main_6" : "F",
                    "main_7" : "G",
                    "main_info": {
                        "main_1" : "H",
                        "main_2" : "I",
                        "main_3" : "J"
                    },
                    "main_4" : "D",
                    "main_5" : "E",  
                    "main_act": [
                        {
                            "main_act_1": 12,
                            "main_act_1_1": "C",
                            "main_act_1_2": "T"
                        },
                        {
                            "main_act_1": 12,
                            "main_act_1_1": "D",
                            "main_act_1_2": "T"
                        }
                    ],
                    "main_8" : "R"
                },
                {
                    "main_4" : "D",
                    "main_5" : "E",
                    "main_10": "P"
                }
            ],
            "main_score": 0.1
        },
        {
           "main_id": "1334",
            "main_sub": [
                {
                    "main_info": {
                        "main_1" : "B",
                        "main_2" : "Q",
                        "main_3" : "C",
                        "main_11": "D"
                    },
                    "main_4" : "S",
                    "main_5" : "E",
                    "main_6" : "G",
                    "main_7" : "T"
                },
                {
                    "main_6" : "F",
                    "main_7" : "G",
                    "main_info": {
                        "main_1" : "A",
                        "main_2" : "I",
                        "main_3" : "J"
                    },
                    "main_10": "W",
                    "main_4" : "D",
                    "main_5" : "E",  
                    "main_act": [
                        {
                            "main_act_1": 112,
                            "main_act_1_1": "C",
                            "main_act_1_2": "T"
                        },
                        {
                            "main_act_1": 123,
                            "main_act_1_1": "D",
                            "main_act_1_2": "T"
                        }
                    ],
                    "main_8" : "R"
                },
                {
                    "main_4" : "D",
                    "main_5" : "E",
                    "main_10": "P"
                }
            ],
            "main_score": 0.1
        }
    ],
    "main_count": 2
}

I am not able to read a large part of this data due to the nesting even after I use $.each in jQuery.

Also is there a way by which I could read every single line of this json data and print it on screen without actually mentioning the field names like "main_1" in my jQuery code ?

Output Expected :

I would just want to print the field names as well as well as their values for each of the two instances of main .

Somewhat like :

main_id - 1234
main_sub -
  main_info - 
    main_1 - A
    main_2 - B

and so on.................. (for both instances of main - thus the main_count = 2 at the end of the json data)

5
  • you dont need $.each to parse this json. May 31, 2013 at 6:58
  • @mithunsatheesh alright so any other method by which i could read through the entire data?
    – stark
    May 31, 2013 at 6:59
  • it would be better if you could tell us what is the output you are expecting. Along with your attempt. May 31, 2013 at 7:00
  • If you have a recursive data structure, you probably need a recursive algorithm, or at least a stack of contexts. May 31, 2013 at 7:01
  • @JanDvorak : i am sorry but i don't get what a stack of contexts means ? Could you please state an example ?
    – stark
    May 31, 2013 at 7:04

3 Answers 3

3

Pure JS solution. This will list whole object in console.

var myVar = {..(your object data here)..};

function listing(obj, prefix){
    for(var key in obj){
        var el = obj[key];

        if(el instanceof Object){
            listing(el, prefix + ' | ' + key);
        }
        else{
            console.log(prefix + ' | ' + key + ': ' + el);
            //document.write(prefix + ' | ' + key + ': ' + el + '<br />'); //alternatively writing to document
        }
    }
}

listing(myVar, '');



Expected output:

 | main | 0 | main_id: 1234
 | main | 0 | main_sub | 0 | main_info | main_1: A
 | main | 0 | main_sub | 0 | main_info | main_2: B
 | main | 0 | main_sub | 0 | main_info | main_3: C
 | main | 0 | main_sub | 0 | main_4: D
 | main | 0 | main_sub | 0 | main_5: E
 | main | 0 | main_sub | 1 | main_6: F
 | main | 0 | main_sub | 1 | main_7: G

and so on..

2
  • I tried this , but it doesn't seem to work , no data being displayed when i changed console.log to instead print it on screen in the body
    – stark
    May 31, 2013 at 7:18
  • If you want to write it to document use document.write(prefix + ' | ' + key + ': ' + el + '<br />');. Are you sure your data is parsed to object already? This could be string.
    – Michael
    May 31, 2013 at 7:19
1

this code is may be help full to you

       success: function(data)
           {
               if(data)
                   {

                        var json = $.parseJSON(data);
                        $(json).each(function(i,val)
                        {
                            //your code  
                            $(val).each(function(index,val)
                             {
                                    //your code
                             });



                       });
                 }
          }
2
  • I am familiar with this structure for extracting json data from a file , but what I am facing a problem with is the code that would go into the $.each nested functions so that I could traverse through the entire data ?
    – stark
    May 31, 2013 at 7:09
  • you know recursive function. try it. May 31, 2013 at 7:14
0

If this json is the result of a request, tel jQuery it should expect json, as an answer, and use the result as a plain javascript object :

$.ajax({
    url: 'my/url',
    dataType: 'json',
    success: function(data) { // 'data' will be an object, not a string
        data.main[0].main_sub[1].main_6; //should be "F"
    }
});

If your json data is stored in a string, use jQuery.parseJSON to turn it into an object :

var data = jQuery.parseJSON(jsonString);
data.main[0].main_sub[1].main_6; //should be "F"
2
  • but by this format , wouldn't I be required to hard code every line of data in order to print it on screen ?
    – stark
    May 31, 2013 at 7:10
  • data.main[0].main_sub[1].main_6; this doesn't work , when i alert it , it says that it is undefined.
    – stark
    May 31, 2013 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.