• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Parsing JSON in Javascript

Question

andrew.p.shay    0

Hello all,
I am new to Javascript and I am trying to parse the following JSON. But nothing happens so I'm assuming there is some error.

I am also trying to figure out how to loop through the JSON and access the data, but I can't even get it to load.

var text = [
    {"ITEM ONE":[
            {"SUB ONE":[
                    "one",
                    "two"
                ]
            },
            {"SUB TWO":[
                    "three",
                    "four"
                ]
            }

    ]},


    {"ITEM TWO":[
            {"SUB THREE":[
                    "five"
                ]
            },
            {"SUB FOUR":[
                    "six"
                ]
            }

    ]}
];

var obj = JSONArray.parse(text);
JSONObject jo = inputArray.getJSONObject(0);
document.getElementById("demo").innerHTML = obj(0);

Here are the variations that I've tried. For var obj, I tried using just JSON.parse(). I also tried not have the jo var.

 

And for the JSON itself, I also tried adding the following to each line 'JSON_HERE'+  To try and deal with the multiline JSON.  I also put it all on one line.

 

I'm not sure what to do. Could someone show me the problem and some code that accesses the element and a snippet that loops through it?

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0
+virtorio    3,133

...

 

 

 

Couple of issues:

 

1. You're assigning a real object to "text", not actual text that needs parsing

2. JSONArray isn't a type, use JSON.parse()

3. You never define an object called "inputArray"

4. Don't provide a type when creating an variable (JSONObject jo)

 

Below is a working demo that parses your JSON (as text) and writes a value from it into a <P> tag.

 

One more thing When testing javascript it's always helpful to load up your browsers developer tools (CTRL SHIFT I in Chrome, F12 in IE) and step though and inspect your code. You can learn a lot from it. 

<!doctype html>
<html>
<head>
 
</head>
<body>
<p id="demo"></p>
 
 
<script>
var text = "[\
    {\"ITEM ONE\":[\
            {\"SUB ONE\":[\
                    \"one\",\
                    \"two\"\
                ]\
            },\
            {\"SUB TWO\":[\
                    \"three\",\
                    \"four\"\
                ]\
            }\
    ]},\
    {\"ITEM TWO\":[\
            {\"SUB THREE\":[\
                    \"five\"\
                ]\
            },\
            {\"SUB FOUR\":[\
                    \"six\"\
                ]\
            }\
    ]}\
]";
 
var obj = JSON.parse(text);
var jo = obj[0];
document.getElementById("demo").innerText = obj[0]["ITEM ONE"][0]["SUB ONE"][0];
</script>
 
</body>
</html>

Share this post


Link to post
Share on other sites
  • 0
Riva    1,214

try text[0][0]; :D

the text variable you wrote is a JSON object, not JSON text so there is no point in parsing it. I dont think JSONArray is a valid JavaScript type

Share this post


Link to post
Share on other sites
  • 0
andrew.p.shay    0

...

 

Thank you very much!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.