-1

Edit: var Mario = '{"Powers":["FireFlower", "Raccoon suit", "Penguin Suit", "Star Powa"],"Color":"Red", "Mustache":"Yes"}'; var inString = prompt("Enter JSON Data to Parse"); var myObjects = JSON.parse(inString); var output = " "; for (t in myObjects) { output += t + " = " + myObjects[t] + "\n"; } console.log(output);

  • 1
    `Mario` is an invalid JSON string. What does it do there at all, you're not using it anywhere? – Bergi Jul 09 '13 at 15:36
  • Also, don't use `document.write` for output. Use `console.log` instead. – Sethen Jul 09 '13 at 15:37
  • My original purpose was to parse the mario string, i understand that it's not being used at the moment and im trying to wrap my head around parsing in general as I've only taken a couple of classes where this wasn't explained. I've done the edit. – user2565115 Jul 09 '13 at 15:55
  • well you're *parsing* it correctly (all levels). For the rest, see the duplicate [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Bergi Jul 09 '13 at 16:26

2 Answers2

0

I suppose you get a:

SyntaxError: Unexpected token ,

?

That's because your Mario-String is not a valid JSON-String. Each element has to have the form of key: value whereas your first element does not have a value.

I guess what you mean is:

var Mario = '{"Powers":["FireFlower", "Raccoon suit", "Penguin Suit", "Star Powa"], "Color":"Red", "Mustache":"Yes"}';
fehnomenal
  • 509
  • 3
  • 10
  • Thanks for fixing that, Though when i input {"Mario":"Powers"} into the prompt is it parsing the whole array? – user2565115 Jul 09 '13 at 15:47
  • 1
    No. It is just parsing the string you provide. So the result of `JSON.parse('{"Mario":"Powers"}')` will be `Object {Mario: "Powers"}`. I'm not really sure what you are trying to accomplish. – fehnomenal Jul 09 '13 at 15:52
  • I have a large amount of data in excel with ID's and such with multiple levels. So for example, i started with Mario as my top and inside mario there are multiple parts. I want to only parse a specific piece of data from the whole thing. and then i would like to create a table to store all the data that i've parsed. The prompt is asking what part of the data the user wants to parse and then input it into this table. Sorry for being so confusing. – user2565115 Jul 09 '13 at 16:03
  • I've been doing this for awhile - that I am aware, there is no mechanism for partial parsing in JSON and doing so properly would be quite a challenge. If you just want mario's powers, just access `JSON.parse(Mario).Powers` or assign the result to something and access the Powers property from there. – aikeru Jul 09 '13 at 16:06
  • 1
    If you want to get at deeper levels in the hierarchy while looping through the objects, I think this is an ideal candidate for recursion. Basically, create a function that loops through the object's properties. Have that function call itself which each property as the argument, until it has crawled through the entire hierarchy. – aikeru Jul 09 '13 at 16:08
  • So I should remove the prompt and parse the variable directly. After parsing i want to create a new table that stores all the data. So what should I do next? – user2565115 Jul 09 '13 at 16:34
0

I would take a look at how JSON objects are formatted.

I think this is what you're looking for in your Mario object:

var Mario = {
    "Powers": ["FireFlower", "Raccoon suit", "Penguin Suit", "Star Powa"],
    "Color": "Red",
    "Mustache": "Yes"
}

This will give you:

Mario.Powers = ["FireFlower", "Raccoon suit", "Penguin Suit", "Star Powa"]

Mario.Color = "Red"

Mario.Mustache = "Yes"

As for what you're trying to accomplish with your prompt, I'm not sure what you're asking.

robert.bo.roth
  • 1,343
  • 3
  • 14
  • 24