1

I am making a web app for nokia S40 platform . I am calling a web service using Javascript that returns the Following JSON

{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http:\/\/bit.ly\/msapps", "long_url": "http:\/\/www.microsoft.com\/web\/gallery\/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } } 

I want to obtain the "short_url" and "long_url " values

I am using eval as var obj = eval ("(" + xmlhttp.responseText + ")");

where xmlhttp.responseText conatains the JSON response. Please help

3 Answers 3

9

tried this and worked

 var s = '{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http://bit.ly/msapps", "long_url": "http://www.microsoft.com/web/gallery/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } } '

 var d = JSON.parse(s);
 console.log(d.data.expand[0].short_url);
 console.log(d.data.expand[0].long_url);
Sign up to request clarification or add additional context in comments.

Comments

1

This expression

JSON.parse('{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http:\/\/bit.ly\/msapps", "long_url": "http:\/\/www.microsoft.com\/web\/gallery\/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } }').data.expand[0].short_url

returns "http://bit.ly/msapps"

Comments

-3

How about this

var json = "{}" // Your JSON string
json = new Function('return ' + json)();
console.log(json.data.expand[0].short_url, json.data.expand[0].long_url);

1 Comment

This is a very bad habit to get into, because in some circumstances it can compromise security. For example, suppose you have javascript code in your server reading JSON from the client. If you do it this way, the client can send any javascript string, and it will run on the server.

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.