1

i am working on parsing a JSONobject. so the thing which i am trying to do is to parse the object using javascript from json and to assign it to an array. any help or suggestions. Thank in advance. here is my code.

my javascript code to parse.

  name = JSON.parse(cooldrinkname);

and one of my json object code is.

       {
            "shopID" : "erer2123",
            "shoname" : "saravana store",
            "cooldrinkname" : "pepsi",
            "cost" : "10"
        },

so from the above code. i am trying to extract the cooldrink name and trying to assing in a array variable name. Thanks in advance.

4

2 回答 2

1

您的 JSON 不是数组,因此您不能将其分配给数组。您应该首先将 JSON 解析为一个对象,然后您可以将cooldrinkname 添加到一个数组中:

var myjson = '{ "shopID" : "erer2123", "shoname" : "saravana store", "cooldrinkname" : "pepsi", "cost" : "10" }';
var obj = JSON.parse(myjson);
var cooldrinknames = [];
cooldrinknames.push(obj.cooldrinkname);
于 2013-05-29T07:19:27.940 回答
0

试试这个,希望它有帮助

var myjson = '{ "shopID" : "erer2123", "shoname" : "saravana store", "cooldrinkname" : "pepsi", "cost" : "10" }';
var cooldrinknames = [JSON.parse(myjson)];
console.log(cooldrinknames); // to view object array in dev. console. 
于 2013-05-29T07:36:27.447 回答