最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert JSON array into individual JS variables - Stack Overflow

programmeradmin1浏览0评论

I have a JSON array:

{"a":"apple,"b":"banana","c":"carrot"}

I want to split each part of the array into seperate variables, ie,

a = "apple",
b = "banana";
c = "carrot";

I have googled my goggles off but can't seem to find a correct way to do this. I am new to JSON and have done a fair bit of reading but what I am after doesn't seem to be referenced within my grasp.

EDIT: There seems to be confusion as to whether my array is a string or object. I am receiving a response from PHP as follows:

$json = array(
    'a' =>  $a,
    'b'     =>  $b,
    'c' =>  $c,
    );
    echo json_encode($json);

My JS code is as follows:

var data = ajax.responseText;
data = JSON.parse(data);

I get {"a":"apple,"b":"banana","c":"carrot"} as a result of

json.stringify(data);

I have a JSON array:

{"a":"apple,"b":"banana","c":"carrot"}

I want to split each part of the array into seperate variables, ie,

a = "apple",
b = "banana";
c = "carrot";

I have googled my goggles off but can't seem to find a correct way to do this. I am new to JSON and have done a fair bit of reading but what I am after doesn't seem to be referenced within my grasp.

EDIT: There seems to be confusion as to whether my array is a string or object. I am receiving a response from PHP as follows:

$json = array(
    'a' =>  $a,
    'b'     =>  $b,
    'c' =>  $c,
    );
    echo json_encode($json);

My JS code is as follows:

var data = ajax.responseText;
data = JSON.parse(data);

I get {"a":"apple,"b":"banana","c":"carrot"} as a result of

json.stringify(data);
Share Improve this question edited Jan 24, 2014 at 6:02 Wildcard27 asked Jan 24, 2014 at 5:20 Wildcard27Wildcard27 1,45921 silver badges51 bronze badges 3
  • 1 Why you want to do it. You can always use obj.a, obj.b, obj.c – Sachin Jain Commented Jan 24, 2014 at 5:22
  • 1 Please note that your question/problem has nothing to do with JSON. After the JSON is parsed, you are working with regular JS arrays and objects. – Felix Kling Commented Jan 24, 2014 at 5:39
  • @Felix Kling Thank you, noted. – Wildcard27 Commented Jan 24, 2014 at 6:11
Add a ment  | 

3 Answers 3

Reset to default 8

One example of how you can do this is found here.

It assumes you want to put the variables into global scope. If this is the case, this will work:

function extract(variable) {
    for (var key in variable) {
        window[key] = variable[key];
    }
}

var obj = {"a":"apple","b":"banana","c":"carrot"}

extract(obj);

alert(a);
alert(b);
alert(c);

In Javascript, arrays are objects, and objects are arrays. But they have been initialized with special behavior.

Take a look at this answer:

https://stackoverflow./a/4215753/1311986

You will find that you want to do something very similar.

First of all {"a":"apple,"b":"banana","c":"carrot"} is not an array, it's a simple JSON String.

Now to parse JSON String you can use

JSON.parse()

It is supported by all Modern Browsers and you can also find the library here

You may try to do this with your app: assuming your jsonString as an array.

 var jsonObj = $.parseJSON("### json string #####");

 for(var index = 0; index < jsonObj.length; index++){
     console.log("a:"+jsonObj[index].a+",b:"+jsonObj[index].b+",c:"+jsonObj[index].c);
 }

You will get the output something like this:

a: apple, b:banana, c:carrot
. . . 
. . .

Hope this solves your problem.

发布评论

评论列表(0)

  1. 暂无评论