Solved

Advanced json just need the record object block

Posted on 2014-10-16
2
124 Views
Last Modified: 2014-12-02
Lets say I have the following JSON OBJECT

{
    "teamname": "Anaheim Ducks",
    "nickname": "Ducks",
    "SportTypes": {
      "-JZLOh7KtM78I5wAL6Ua": true
    },
    "locations": {
      "-JZLCFtpcuvsYxb5N1Cc": true
    },
    "countries": {
      "-JZG9IwEwKGqsExky7UT": true
    },
    "leagues": {
      "-JZLd1hXyyr9ehLwmmbc": true
    },
    "Divisions": {
      "-JZLh2UYKi5IvphLtX4I": true,
      "-JZLh2UaZbpcO7XixD1x": true
    }
  }

Open in new window



I want to loop thru it and pull the record chunks so if the loop pointer position is at 0 all the following JSON chunk will be stored in a variable.

{
    "teamname": "Anaheim Ducks",
    "nickname": "Ducks",
    "SportTypes": {
      "-JZLOh7KtM78I5wAL6Ua": true
    },
    "locations": {
      "-JZLCFtpcuvsYxb5N1Cc": true
    },
    "countries": {
      "-JZG9IwEwKGqsExky7UT": true
    },
    "leagues": {
      "-JZLd1hXyyr9ehLwmmbc": true
    },
    "Divisions": {
      "-JZLh2UYKi5IvphLtX4I": true,
      "-JZLh2UaZbpcO7XixD1x": true
    }
  }

Open in new window

0
Comment
Question by:stargateatlantis
2 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 40385920
What didn't this do? http://jsbin.com/sunica/1/edit?js,console

the following would store the chunk in a variable
var chunk = myobject[0];
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 40411823
If you parse the the JSON string into variable then is that variable the variable that you asked for. So what is the question?

Here a loop proving that the generated object from your JSON string gets all the properties as above stated:
<script>
var strText = '{\
    "teamname": "Anaheim Ducks",\
    "nickname": "Ducks",\
    "SportTypes": {\
      "-JZLOh7KtM78I5wAL6Ua": true\
    },\
    "locations": {\
      "-JZLCFtpcuvsYxb5N1Cc": true\
    },\
    "countries": {\
      "-JZG9IwEwKGqsExky7UT": true\
    },\
    "leagues": {\
      "-JZLd1hXyyr9ehLwmmbc": true\
    },\
    "Divisions": {\
      "-JZLh2UYKi5IvphLtX4I": true,\
      "-JZLh2UaZbpcO7XixD1x": true\
    }\
  }';
  
var obj = JSON.parse(strText);

var msg = "";
for(var elem in obj){
  if(typeof obj[elem] == "object"){
    msg += elem+" {\n";
	for(var attr in obj[elem]){
	  msg += attr+": "+obj[elem][attr]+"\n";
	}
	msg += "}\n";
  } else {
    msg += elem+": "+obj[elem]+"\n";
  }
}

alert(msg);

</script>

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question