9

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'}]
user962206
  • 15,637
  • 61
  • 177
  • 270
  • 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 Jan 30 '13 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 Jan 30 '13 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 Jan 30 '13 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 Jan 30 '13 at 10:23
  • Hmmm Well spotted @AnthonyGrist. – Gavin Jan 30 '13 at 10:26
  • Can you show your server side code? If you want to go the lazy guy's way just parse twice your result, or, choose the wise man way and correct the faulty server side code. – Adrian Salazar Jan 30 '13 at 10:35
  • @AdrianSalazar There I have posted it already, the loop that creates the Javascript object – user962206 Jan 30 '13 at 10:50
  • So where is the "tag: property coming from? – Adrian Salazar Jan 30 '13 at 10:52
  • 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 Jan 30 '13 at 10:54

2 Answers2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The `tags` property in that JSON is a string containing an array definition, it's not actually an array. – Anthony Grist Jan 30 '13 at 10:21
  • Oh, you are right. I have noticed that. Thanks for pointing this out. I will updated my answer. – Darin Dimitrov Jan 30 '13 at 10:23
  • I tried that using this but I received this Uncaught TypeError: Cannot read property 'tags' of null – user962206 Jan 30 '13 at 10:25
  • I guess that's because you do not have valid JSON inside the `tags` property. You will need to fix your server side code so that it returns valid JSON. In a valid JSON string property names must be enclosed in double quotes. I have updated my answer to reflect on that. – Darin Dimitrov Jan 30 '13 at 10:26
  • @MCL, your example is wrong. You have quotes the entire `json` variable. Try with the json variable shown by the OP. It won't work. – Darin Dimitrov Jan 30 '13 at 10:39
  • @Darin Given, that the json is already parsed, it's even easier: [http://jsfiddle.net/XTccG/1/](http://jsfiddle.net/XTccG/1/) – MCL Jan 30 '13 at 10:44
  • Yes, you are using `eval`, exactly as I have shown in my answer. I don't see how this is different than what I explained. – Darin Dimitrov Jan 30 '13 at 10:45
  • I am aware of that. This is why I posted the fiddle as a comment to your answer. Essentially, there is no difference, jsfiddle helps other people to understand what code does. This is why you can see fiddles for given solutions frequently. – MCL Jan 30 '13 at 10:49
0
initSelection: function (element, callback) {
                    var data = $(element).val();
                    callback($.parseJSON(data));
                }