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'}]
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. 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. 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. 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. 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. Commented Jan 30, 2013 at 10:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

The tags property in that JSON is a string containing an array definition, it's not actually an array.
Oh, you are right. I have noticed that. Thanks for pointing this out. I will updated my answer.
I tried that using this but I received this Uncaught TypeError: Cannot read property 'tags' of null
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.
@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.
|
0
initSelection: function (element, callback) {
                    var data = $(element).val();
                    callback($.parseJSON(data));
                }

1 Comment

Please add some explanation as to what this code is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.