javascript - How to reference specific field in this JSON? - Stack Overflow

trying to reference a field in this JSON but it's unlike anything in the examples I've been l

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
Add a ment  | 

3 Answers 3

Reset to default 5

With 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信