开发者

Extracting values with Javascript

开发者 https://www.devze.com 2023-03-28 17:17 出处:网络
I have a variable called \"result\", var result; that result value is equal to following value, please presume that is just a string :)

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",
           开发者_JS百科  "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.


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);


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.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消