Solved

Advanced json just need the record object block

Posted on 2014-10-16
2
120 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now