0

I Have this kind of JSON Object

"{\"date\": \" 9 Marzo\", \"time\": \" 07:00 - 13:20\", \"descrizione\": \" concerto\",   \"alimenti\": [{ \"macchina\":\"si\", \"pollo\":\"no\" }] }";

I want to get exactly the string "macchina" and "pollo", which are the keys text/value (I get the Object from an ajax, so "9 Marzo" would be like response.date), and same for "si" and "no", I cannot arrive to them. I have tryed console.log(response.alimenti[i][0]); but it's undefined.

i come from the cicle: for (i = 0; i < response.ruoli.length; i++)

2
  • did you try to use JSON.parse? Commented Mar 13, 2015 at 15:05
  • If I parse it, how can I arrive to get "pollo" or "no"? Commented Mar 13, 2015 at 15:25

3 Answers 3

1

This will get you to the strings "macchina" and "pollo":

var json = "{\"date\": \" 9 Marzo\", \"time\": \" 07:00 - 13:20\", \"descrizione\": \" concerto\",   \"alimenti\": [{ \"macchina\":\"si\", \"pollo\":\"no\" }] }";
var obj = JSON.parse(json);
for (var k in obj.alimenti[0]) {
   console.log(k);
}

or their values:

for (var k in obj.alimenti[0]) {
   console.log(obj.alimenti[0][k]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think this going to be what I want, I'll make some other tryes
is there a way to go directly to the value? without the for cycle?
@Trink You can go directly only if you know the key. For this example: obj.alimenti[0]['macchina'] or obj.alimenti[0].macchina will both get you "si".
Ok thank, I have changed the JSON structure; but you deserve the upvote cause U only full understood me ;)
1

You'd be better off parsing the JSON object and then extracting the string from the javascript object.

i.e var obj = JSON.parse("{\"date\": \" 9 Marzo\", \"time\": \" 07:00 - 13:20\", \"descrizione\": \" concerto\",   \"alimenti\": { \"macchina\":\"si\", \"pollo\":\"no\" } }";);
console.log(obj.alimenti[0].macchina);

or pollo

console.log(obj.alimenti[0].pollo);

Also, that object structure is a little weird. You might want to remove the array from within the alimenti to better access the data.

1 Comment

I need the array to iterate the datas, because is not supposed I know the name of the key, I litterally want to know the name of the key, so I cannot write .pollo to find "pollo", maybe concept not clear, take referiment to @wojciechpolak answer
1

Using response.alimenti[i][0] won't work because alimenti is an array of object(s), not an array of arrays.

This instead:

var alimenti = response.alimenti[0];
console.log(alimenti.maccina);
console.log(alimenti.pollo);

Example: http://jsfiddle.net/zcuwfb9s/

2 Comments

Ok, maybe I have to do a different JSON, because it is supposed that I dont know the name of "alimenti" before getting them, obj.alimenti[0].pollo presume that I know that the JSON contains the property key pollo, Maybe I'm using a bad approach, I should go for {"alimenti" : ["macchina", "pollo"], "alimentiDetail" : ["si", "no"]
Makes sense. I'd suggest {'alimenti' : [{'name':'macchina', 'value':'si'}, {'name':'pollo', 'value':'no'}]} so you can keep both name+value in one same grouping (object).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.