1

I get this html as response when i post api call, so i want to know how i can read client_id and session_token with jquery or some simple javascript code??

function getNewsFeed(d) {
    var c = {
        page_num: b,
        num_events: d,
        return_as_html: true,
        source: "web"
    };
    TAGGED.api.call(c, check)
}

Now want to check things:

function check() {
    // checking here
}

["{\"stats\":\"ok\",\"dia\":\"44\",\"result\":{\"success\":true,\"session_token\":\"969ndg1ai0c43034\",\"next_event_id\":0,\"client_id\":1314852}}"]

I dont have JSON so cant read it with JSON.parse i tried using but in firebug it does not show JSON

i get same code in response and html when i check it with firebug.

Thanks.

13
  • Please post the code you are using to make the call so we can answer better. Commented Oct 14, 2012 at 6:58
  • 2
    You don't have JSON as in, you're using IE6 or what? JSON.parse is part of any modern browser implementation. If the [] are part of the response, pass the first item of the array to JSON.parse. Commented Oct 14, 2012 at 7:02
  • Why you can't use JSON.parse? Commented Oct 14, 2012 at 7:04
  • 2
    If you're accepting jQuery, just use $.parseJSON() but if the native JSON parser didn't work, your problem is probably elsewhere. Commented Oct 14, 2012 at 7:06
  • 1
    The response is not HTML, as you said yourself, it looks like JSON. Even if the browser does not support the native JSON interface, you can always include json2.js. Note that the response is an array with a string where the string itself contains JSON as well. So you have to parse the response twice, once to get the array and then the string inside the array. Commented Oct 14, 2012 at 7:11

1 Answer 1

2

If you can be sure there are not going to be certain special characters in your values, you can split by comma, then by colon, and strip out non-matching characters...

str = '["{\"stats\":\"ok\",\"dia\":\"44\",\"result\":{\"success\":true,\"session_token\":\"969ndg1ai0c43034\",\"next_event_id\":0,\"client_id\":1314852}}"]'

// replace all characters that are not letters, numbers, underscore, colon or comma
str = str.replace(/[^a-zA-Z0-9_:,]/g,'')

// get array of key value pairs
all = str.split(',')

// create empty opts object to store final data
opt = {}

// loop through array of key value pairs
for(i in all){

    // split into key value array
    bits = all[i].split(/:/)

    // assign to opt object
    opt[bits[0]] = bits[1]

}

// access values vie keys of opt
alert(opt.client_id+": "+opt.session_token)
Sign up to request clarification or add additional context in comments.

2 Comments

im not able to get this html in codes. my question how to read them.. first i need get it and then i can replace it with your codes
@deerox: The response is probably passed as argument to the callback, i.e check. Doesn't the API you use provide documentation with examples? Maybe they even already parse the data for you and you asked the whole question for nothing...

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.