This is a migrated thread and some comments may be shown as answers.

[Solved] Trying to parse childnodes in Json

1 Answer 25 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andreas Kviby
Top achievements
Rank 1
Andreas Kviby asked on 18 Nov 2010, 09:23 AM
Hey
I have this json Data in a string.
{
   "Contacts" :
      [
         {
            "CompanyPerson" : "Tia Bruchetta",
            "CompanyNumber" : "10001",
            "Cr" :
               [
                  {
                     "Data" : "info@foretaget.se",
                     "ComKod" : "7"
                  }
               ]
         },
         {
            "CompanyPerson" : "Matias Albrecht",
            "CompanyNumber" : "501167",
            "Cr" :
               [
                  {
                     "Data" : "016-137180",
                     "ComKod" : "3"
                  },
                  {
                     "Data" : "info@foretaget.se",
                     "ComKod" : "8"
                  }
               ]
         }
   ]
}

And now I need to parse this JSON string to extract the Data and ComKod which is a child object to the Cr node as you can see. How can I extract that information?
So far I came up with this...
for(var i=0; i < json.Contacts.length;i++) {
  var header = json.Items[i];
  var child = header.Cr[0].toString()
}

Please help me

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 18 Nov 2010, 09:36 AM
Hello Andreas Kviby,

In order to use a JSON string you need to deserialize it first. You can use the json2 JavaScript library to do so. After you have the string correctly deserialized as a JavaScript object you can traverse it like this:

var data = JSON.parse(json);
 
for (var i = 0; i < data.Contacts.length; i++)
{
      var contact = data.Contacts[i];
      var child = contact.Cr[0];
      var comKod = child.ComKod;
}

I hope this helps,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Andreas Kviby
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or