0

here is the code

var txt = '{"tblCommoHier":[ {"DEPT":[' + '{"DEPT":"100","DEPT_NAME":"Collectibles" },' + '{"DEPT":"105","DEPT_NAME":"Commodities" },' + '{"DEPT":"140","DEPT_NAME":"Souvenir" }]}]}';              
var obj = eval ("(" + txt + ")");

I'm trying the code

obj.tblCommoHier.DEPT[1].DEPT

to reach out to the first element of the DEPT element under tblCommoHier but I keep getting error says not defined.

Can somebody please help me with this issue?

buri kuri
  • 429
  • 3
  • 7
  • 19

4 Answers4

4

In general, avoid using eval. The JSON object has a parse method for converting from strings to JSON. Also, when dereferencing an object nested in an array, you must remember your array indexing. The first two layers of your JSON object have array values. The correct formulation is:

var txt = '{"tblCommoHier":[ {"DEPT":[' + '{"DEPT":"100","DEPT_NAME":"Collectibles" },' + '{"DEPT":"105","DEPT_NAME":"Commodities" },' + '{"DEPT":"140","DEPT_NAME":"Souvenir" }]}]}';

var obj = JSON.parse(txt);

var elem = obj.tblCommoHier[0].DEPT[0].DEPT;

This yields "100".

nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • 1
    Yes, OP should use `JSON.parse()` instead of `eval()` – Mike Brant Dec 26 '12 at 17:32
  • Hi, how to structure the JSON to access the items using obj.tblCommoHier.DEPT[1].DEPT. The reason I ask this is because I don't want to loop through the items in the JSON array to reach specifically to the tblCommoHier.DEPT items. – buri kuri Dec 26 '12 at 17:39
  • @burikuri Like [this](http://jsfiddle.net/uEE82/). Remember that JavaScript arrays start at index 0 though. The first element is accessed through array[0]. – nbrooks Dec 26 '12 at 17:43
  • @nbrooks say I have a subelement called CLASS under tblCommoHier, should I still need to reach out any element under the CLASS like: obj.tblCommoHier[1].CLASS[0].DEPT ? – buri kuri Dec 26 '12 at 18:05
  • and say you wanna access an element that you don't know the index in the array, do you still have to loop through all items? – buri kuri Dec 26 '12 at 18:12
  • @burikuri If you don't know the index you have to loop through the array and check each one. As for your question about `CLASS`, I'm not sure what structure you mean. Make a [jsFiddle](http://jsfiddle.com) with the JSON and send the link – nbrooks Dec 26 '12 at 18:15
  • 1
    @MikeBrant `$.parseJSON` uses `JSON.parse` internally, but it is not universally available; if he is already using jQuery the former would be preferred. – Explosion Pills Dec 26 '12 at 18:22
  • 1
    @ExplosionPills My comment was really about using JSON parsing in general rather than `eval()` you could absolutely use `$.parsJSON()` here as well. – Mike Brant Dec 26 '12 at 18:28
  • @burikuri `JSON.parse` doesn't work there because the object is already JSON, it isn't a string. No need to parse it. Pretend `eval` doesn't exist. [`eval` is evil](http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil). `obj.tblCommoHier.CLASS[0].CLASS` [gives the first class](http://jsfiddle.net/uEE82/4/). – nbrooks Dec 26 '12 at 21:59
  • @nbrooks [jsfiddle](http://jsfiddle.net/uEE82/5/) so in thise case, if different variables are required to create a JSON string, I need to use eval like case above, or not? – buri kuri Dec 27 '12 at 15:11
  • @burikuri You should *never* use `eval` to parse JSON. Use `JSON.parse` or `$.parseJSON`. *Forget* `eval`. – nbrooks Dec 27 '12 at 15:13
  • @nbrooks If I don't use eval, how to convert a text file(using variables) into an JSON string like in this [example](http://jsfiddle.net/uEE82/5/)? – buri kuri Dec 27 '12 at 15:17
  • 1
    Use `JSON.parse`! You simply remove `eval` and replace it with `JSON.parse`. Like the name suggests, it's designed for parsing JSON. The usage is shown in my answer above. As is, that example doesn't look like valid JSON to me, but I've shown you examples converting from JSON strings to objects above. If you're building the string in parts it's exactly the same, once the resulting string is a valid JSON string. Make sure you have all your quotation marks and you're lining up your braces appropriately. [`JSON.parse`](http://mzl.la/RAYI94) will take care of the rest. Check the docs for details. – nbrooks Dec 27 '12 at 15:24
2

You want

obj.tblCommoHier[0].DEPT[0].DEPT

That will yield "100"

If you are using jQuery you should use $.parseJSON instead of eval.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
2

tblCommoHier is an array itself, so you should use:

obj.tblCommoHier[0].DEPT[1].DEPT

Take a look at this jsFiddle if you want to test anything

aurbano
  • 3,324
  • 1
  • 25
  • 39
0

try this: http://jsfiddle.net/jbTLB/

var txt = '{"tblCommoHier":[ {"DEPT":[' + '{"DEPT":"100","DEPT_NAME":"Collectibles" },' + '{"DEPT":"105","DEPT_NAME":"Commodities" },' + '{"DEPT":"140","DEPT_NAME":"Souvenir" }]}]}';
var obj = $.parseJSON(txt);
console.log(obj.tblCommoHier[0].DEPT[0].DEPT);
    //-----------------------------------^------this will get the 100
Jai
  • 74,255
  • 12
  • 74
  • 103