trying to reference a field in this JSON but it's unlike anything in the examples I've been looking at so I'm kinda stumped.
I want to know how to reference the field at the end "HOW DO I REFERENCE THIS?". Thanks for any help.
var JSON =
{ "employees": {
"name" : "david",
"car" : "audi"
},
{
"name" : "jimmy",
"car" : "VW"
},
"customers" : {
"name" : "philip",
"purchase": "cabbage"
},
{
"name" : "Helen",
"purchase": "HOW DO I REFERENCE THIS?"
}
}
var x = "HOW DO I REFERENCE THIS?";
trying to reference a field in this JSON but it's unlike anything in the examples I've been looking at so I'm kinda stumped.
I want to know how to reference the field at the end "HOW DO I REFERENCE THIS?". Thanks for any help.
var JSON =
{ "employees": {
"name" : "david",
"car" : "audi"
},
{
"name" : "jimmy",
"car" : "VW"
},
"customers" : {
"name" : "philip",
"purchase": "cabbage"
},
{
"name" : "Helen",
"purchase": "HOW DO I REFERENCE THIS?"
}
}
var x = "HOW DO I REFERENCE THIS?";
Share
asked Dec 31, 2013 at 15:10
user3141323user3141323
331 silver badge4 bronze badges
2
-
3
You don't, since you've coded up a syntax error :) What I'm saying is that your object literal is not valid. You can't have
{ }
blocks without property names. – Pointy Commented Dec 31, 2013 at 15:10 - Suggested reading: There's no such thing as a "JSON Object." If valid, your snippet would be entirely JavaScript. Not JSON. They are certainly related, but not the same. – Jonathan Lonowski Commented Dec 31, 2013 at 15:19
3 Answers
Reset to default 5With the right syntax, which I believe you want this:
var JSON = {
"employees": [
{
"name": "david",
"car": "audi"
},
{
"name": "jimmy",
"car": "VW"
}],
"customers": [
{
"name": "philip",
"purchase": "cabbage"
},
{
"name": "Helen",
"purchase": "HOW DO I REFERENCE THIS?"
}]
}
You can get purchase
by using this:
for (var i in JSON.customers)
{
var customer = JSON.customers[i];
var value = customer.purchase; // cabbage, HOW DO I...
}
try this:
var JSON = {
"employees": [{
"name": "david",
"car": "audi"
}, {
"name": "jimmy",
"car": "VW"
}],
"customers": [{
"name": "philip",
"purchase": "cabbage"
}, {
"name": "Helen",
"purchase": "HOW DO I REFERENCE THIS?"
}]
}
$.each(JSON.customers, function (k, data) {
alert(data.purchase);
});
Working Fiddle
Do you need to find the purchase string for "Helen" or just the second customer, if it's the second customer it will be (with valid JSON):
var string = '{"employees": [{"name": "david", "car": "audi"}, {"name": "jimmy", "car": "VW"}], "customers": [{"name": "philip", "purchase": "cabbage"}, {"name": "Helen", "purchase": "HOW DO I REFERENCE THIS?"}]}';
var string = JSON.parse(string);
console.log(string['customers'][1]['purchase']);
and if you need to find "Helen" it would be something like this (with valid JSON):
var string = '{"employees": [{"name": "david", "car": "audi"}, {"name": "jimmy", "car": "VW"}], "customers": [{"name": "philip", "purchase": "cabbage"}, {"name": "Helen", "purchase": "HOW DO I REFERENCE THIS?"}]}';
var string = JSON.parse(string);
for (var i=0 ; i < string['customers'].length ; i++) {
if (string['customers'][i]["name"] == "Helen") {
var result = string['customers'][i]['purchase'];
}
}
console.log(result);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744810192a4595011.html
评论列表(0条)