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":"admin@website.com",
             "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.

Winthan Aung
  • 351
  • 1
  • 4
  • 11
XML guy
  • 369
  • 4
  • 6
  • 15

4 Answers4

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);
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • 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). – Jamie Dixon Aug 16 '11 at 15:55
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.

Liam Newmarch
  • 3,935
  • 3
  • 32
  • 46
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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
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);
Community
  • 1
  • 1
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113