admin管理员组

文章数量:1359237

I have a string: items[0].name that I want to apply to a JSON object: {"items":[{"name":"test"}]} which is contained in the variable test. I want to apply that string to the object in order to search it (test.items[0].name). I can only think of one way to do this: parse the square brackets and dots using my own function. Is there another way I can do this? Perhaps using eval? (Even though I'd LOVE to avoid that...)

For clarity: I have a JSON object, what is inside of it is really irrelevant. I need to be able to query the object like so: theobject.items[0], this is normal behaviour of a JSON object obviously. The issue is, that query string (ie. items[0]) is unknown - call it user input if you like - it is literally a string (var thisIsAString = "items[0]"). So, I need a way to append that query string to theobject in order for it to return the value at theobject.items[0]

I have a string: items[0].name that I want to apply to a JSON object: {"items":[{"name":"test"}]} which is contained in the variable test. I want to apply that string to the object in order to search it (test.items[0].name). I can only think of one way to do this: parse the square brackets and dots using my own function. Is there another way I can do this? Perhaps using eval? (Even though I'd LOVE to avoid that...)

For clarity: I have a JSON object, what is inside of it is really irrelevant. I need to be able to query the object like so: theobject.items[0], this is normal behaviour of a JSON object obviously. The issue is, that query string (ie. items[0]) is unknown - call it user input if you like - it is literally a string (var thisIsAString = "items[0]"). So, I need a way to append that query string to theobject in order for it to return the value at theobject.items[0]

Share Improve this question edited Nov 13, 2012 at 3:52 iLoch asked Nov 13, 2012 at 3:41 iLochiLoch 7693 gold badges13 silver badges33 bronze badges 2
  • Whaa? Update your question to include (1) Your original object, (2) What you are applying and (3) What the end result should look like. I'm really not following what you are trying to do here. – Jeremy J Starcher Commented Nov 13, 2012 at 3:46
  • Note that there's no such thing as a JSON object. – nnnnnn Commented Nov 13, 2012 at 5:33
Add a ment  | 

2 Answers 2

Reset to default 8
function locate(obj, path) {

  path = path.split('.');
  var arrayPattern = /(.+)\[(\d+)\]/;
  for (var i = 0; i < path.length; i++) {
    var match = arrayPattern.exec(path[i]);
    if (match) {
      obj = obj[match[1]][parseInt(match[2])];
    } else {
      obj = obj[path[i]];
    }
  }

  return obj;
}

var name = locate(test, 'items[0].name');

...JSON doesn't have objects, it's just a string. If you're dealing with an object (ie: you can reference it using dot/bracket notation) then it's just a JavaScript object/array...

So depending on what the deal is, if you're dealing with a 100% string:

'{"name":"string","array":[0,1,2]}'

Then you need to send it through JSON.parse;

var json_string = '{"name":"string","array":[0,1,2]}',
    js_obj = JSON.parse(json_string);

js_obj.name; // "string"
js_obj.array; // [0,1,2]
js_obj.array[1]; // 1

If it's not a string, and is indeed an object/array, with other objects/arrays inside, then you just need to go:

myObj.items[0].name = items[0].name;

If it IS a string, then .parse it, and use the parsed object to do exactly what I just did. If it needs to be a string again, to send to the server, then use JSON.stringify like:

var json_string = JSON.stringify(js_obj);

Now you've got your modified JSON string back.

If you need to support GhettoIE (IE < 8), then download json2.js from Douglas Crockford, and add that script on the page conditionally, if you can't find window.JSON.

本文标签: Javascript How to convert JSON dot string into object referenceStack Overflow