0

This is my sample JSON file , which im trying to parse and read the values ....

C = {{
    "Travel": {
        "ServiceProvider": {
            "Name": "SRS",
            "Rating": "3 stars",
            "Rates": "Nominal",
            "Features": {
                "OnlineBooking": "Yes",
                "SMS_Ticket": "No"
            },
            "UserDetails": {
                "Name": "Jack",
                "Age": "33",
                "Gender": "Male"
            }
        },
        "BusProvider": {
            "Name": "SRS",
            "Rating": "3 stars",
            "Rates": "Nominal",
            "Features": {
                "OnlineBooking": "Yes",
                "SMS_Ticket": "No"
            },
            "UserDetails": {
                "Name": "Jack",
                "Age": "33",
                "Gender": "Male"
            }
        }
    }
}

I'm pretty new to JS , and i need to access the nested elements in a generic fashion.

Im not able to extract the details properly. Im getting stuck accessing nested the child elements.

  1. The problem for me is that i wont always know the names of the "key's' to acess them , the JSON will be dynamic , hence i need a generic mechanism to acess the nested child elements. The Nesting can go upto 3 -4 levels.

  2. what notation do we use to access the key / value pairs when the nesting is deep.

Any Help would be appreciated.

WonderBoy
  • 279
  • 1
  • 3
  • 7
  • how are you planning to show these elements?by alert? – Cris Jan 23 '13 at 11:26
  • I actually need to build an object out of these JSON. I have added the alert just for example sake .. – WonderBoy Jan 23 '13 at 11:27
  • refer http://stackoverflow.com/questions/4104321/recursively-parsing-json – Cris Jan 23 '13 at 11:28
  • I don't get it, after `var Data = JSON.parse(c);` Data will contain everything easly accessible (e.g. `Data.Travel.BusProvider.Name === 'SRS'`). Why would you need another parse method? – Yoshi Jan 23 '13 at 11:31
  • possible duplicate of [Parsing Complex JSON in JS or jQuery](http://stackoverflow.com/questions/14457366/parsing-complex-json-in-js-or-jquery) -- you already asked this question and it was closed as duplicate. In that other question it is explained how you can iterate over an object using `for...in`. I will update it to mention recursion as well though. – Felix Kling Jan 23 '13 at 11:43
  • @ Yoshi : Yes i understand that once it is parsed , everything will be available. But my problem is not with parsing , but with accessing. And also as i mentioned , i wont always know the name of the element to access it as you have mentioned. – WonderBoy Jan 23 '13 at 11:44

2 Answers2

0

ater desirializing your object you can do this

var resultJSON = '{"name":"ricardo","age":"23"}';
var result = $.parseJSON(resultJSON);
$.each(result, function(k, v) {
    //display the key 
    alert(k + ' is the key)
}

you can do it using recursively offcourse like this - Link Here the way is the same just adapt to your example

Ricardo Vieira
  • 730
  • 7
  • 13
0

For dynamic access you can use brackets notation i.e. var json = {nonKnown: 1}; now you can access it like that: var unknowPropertyName = "nonKnown"; var value = json[unknownPropertyName];

But if you can not even define dynamically name of the property, then you should use

for(variableName in json){
  if(json.hasOwnProperty(variableName)){
    console.log(variableName);
  }
}

You should get the basic idea from this. Good luck

Vytautas Butkus
  • 5,365
  • 6
  • 30
  • 45