Solved

Advanced json just need the record object block

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

Accepted Solution

by:
Rob 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 6 20
How do I show metrics with PHP or Javascript 6 31
Do alert on select 6 13
Possible propagation problem 1 19
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

808 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