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

jquery - Convert JSON To Array Javascript - Stack Overflow

programmeradmin1浏览0评论

I am currently receiving a JSON Object From the Server side of my application, the result is this

{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}

But then I don't really need the "tags" and the double quotes in the result.

So what I want is an array representation of that JSON object

therefore how would I convert this

{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}

to this

[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]

Here's the loop that creates the array

String k = "["; 
        List<Tag> tg = audioTaggingService.findTagsByName(q);
        for(int i = 0; i<audioTaggingService.findTagsByName(q).size();i++){
            Tag t = tg.get(i);
            if(i == (tg.size() - 1)){
                k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
            }else{
                k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
            }
        }
        k+="]";

The result of the code above is this

[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]

I am currently receiving a JSON Object From the Server side of my application, the result is this

{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}

But then I don't really need the "tags" and the double quotes in the result.

So what I want is an array representation of that JSON object

therefore how would I convert this

{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}

to this

[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]

Here's the loop that creates the array

String k = "["; 
        List<Tag> tg = audioTaggingService.findTagsByName(q);
        for(int i = 0; i<audioTaggingService.findTagsByName(q).size();i++){
            Tag t = tg.get(i);
            if(i == (tg.size() - 1)){
                k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
            }else{
                k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
            }
        }
        k+="]";

The result of the code above is this

[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]
Share Improve this question edited Jan 30, 2013 at 10:49 user962206 asked Jan 30, 2013 at 10:16 user962206user962206 16.1k65 gold badges184 silver badges272 bronze badges 9
  • 3 From your example, you can simply access obj.tags (where obj is your JSON object) which will return an array of objects each containing a value and label property. – Gavin Commented Jan 30, 2013 at 10:17
  • @Gavin could you provide an example? I need the second version because I am currently using the JQuery TagIt plugin that needs the tagSource to be an array. – user962206 Commented Jan 30, 2013 at 10:19
  • 2 @Gavin No, it won't. The tags property is a string, as evidenced by the double-quotes surrounding all of its content. The contents of that string isn't valid JSON, but is a valid definition for an array in JavaScript. – Anthony Grist Commented Jan 30, 2013 at 10:21
  • 1 If you can, you'd be much better off changing your server-side code so that it returns an actual JSON array, rather than a string. – Anthony Grist Commented Jan 30, 2013 at 10:23
  • 1 @AdrianSalazar On Send the Server appends it, I have figured out the problem now. its the values, they are in single quotes. I need to make them double quotes. – user962206 Commented Jan 30, 2013 at 10:54
 |  Show 4 more comments

2 Answers 2

Reset to default 10

Assuming you got your server side response in a javascript object called response you could parse the tags string property using the $.parseJSON function. But first you will need to fix your server side code so that it returns a valid JSON string for the tags property (in JSON property names must be enclosed in quotes):

// This came from the server
var response = {"tags":"[{\"value\": 2,\"label\": \"Dubstep\"},{\"value\": 3,\"label\": \"BoysIIMen\"},{\"value\": 4,\"label\":\"Sylenth1\"}]"};

// Now you could parse the tags string property into a corresponding
// javascript array:
var tags = $.parseJSON(response.tags);

// and at this stage the tags object will contain the desired array
// and you could access individual elements from it:
alert(tags[0].label);

If for some reason you cannot modify your server side script to provide a valid JSON in the tags property you could still use eval instead of $.parseJSON:

var tags = eval(response.tags);

It's not a recommended approach, normally you should avoid using eval because it will execute arbitrary javascript.

initSelection: function (element, callback) {
                    var data = $(element).val();
                    callback($.parseJSON(data));
                }
发布评论

评论列表(0)

  1. 暂无评论