0

I have a variable called "result",

var result;

that result value is equal to following value, please presume that is just a string :)

---------result value -----------

for (;;);{
     "send":1,
     "payload":{
         "config":{
             "website":"",
             "title":"welcome to site",
             "website-module":1313508674538,
             "manufatureid":"id.249530475080015",
             "tableid":"id.272772962740259",
             "adminid":100002741928612,
             "offline":null,
             "adminemail":"[email protected]",
             "adminame":"George",
             "tags":"web:design:template",
             "source":"source:design:web",
             "sitelog":[],
             "errorlog":0,
             "RespondActionlog":0,
             "map":null
           },
        "imgupload":""
     },
     "criticalerror":[0],
     "report":true
 }

---------result value------------

From that value, I would like to extract tableid which is "id.272772962740259" with classic Javascript.

How can I extract the code, please let me know how can i do with simple javascript, please don't use Jquery, all I just need is simple javascript.

4
  • Is it JSON or string? Why the empty for loop in front? Commented Aug 16, 2011 at 15:50
  • 1
    Is "result" a string containing that code? And are you using jQuery or any other library? Could make this a lot easier... Commented Aug 16, 2011 at 15:51
  • result value is respond from Ajax respond. All I just need is to extract the value of tableid and assign to another variable. Commented Aug 16, 2011 at 15:52
  • 1
    please, presume that the result is a string. we can't remove "for (;;);" Commented Aug 16, 2011 at 16:06

4 Answers 4

2

You can simply evaluate the value of the variable to obtain the values. However, please note that your current value is not valid JSON; that for(;;); at the beginning of the value invalidates the format. Remove that, and you can do this:

var object = eval('(' + resultMinusThatForLoop + ')');

alert(object.payload.config.tableid);
Sign up to request clarification or add additional context in comments.

1 Comment

Just got there before me. It's worth noting that future versions of ECMAScript may contain a JSON parser however right now, eval seems to be the best solution (other than using a JSON parse in a framework).
1

If that data is a string the parse it with a JSON parse. The following should get the value you want

JSON.parse(result).payload.config.tableid; // "id.272772962740259"

Edit: though, as Tejs says, the for(;;) invalidates the string and stops it from being parsed. If you can remove that, do.

Comments

0

You need to remove the empty for loop, then parse the string. DO NOT use eval; most modern browsers provide built-in JSON-parsing facilities, but you can use json2.js if yours does not. Assuming that you assign the results of parsing the JSON to result, you should be able to get that value using result.payload.config.tableid.

You should probably read a good JS reference. JavaScript: The Good Parts or Eloquent JavaScript would be a good choice.

Comments

0

If result is a javascript object and not a string, you can just use 'result.payload.config.tableid'.

If it is not, how do you get the AJAX result? Are you using XmlHttpRequest directly? Most libraries will give you a javascript object, you might be missing a flag or not sending the response back with the right content type.

If it is a string and you want to parse it manually, you should use a JSON parser. Newer browsers have one built in as window.JSON, but there is open source code for parsing it as well.

var obj = JSON.parse(result);
alert('tableid is ' + obj.payload.config.tableid);

1 Comment

i am using XmlHttpRequest in order to get that result.

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.