1
    var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]'
    var parsedJSON = eval('(\'+JSONObj+\')');    
    var result=parsedJSON.result;
    var count=parsedJSON.count;
    alert('result:'+result+' count:'+count);

in alert its giving undefined :undefined

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Felix Kling Mar 13 '13 at 08:27
  • Hi,I think you are new here, see,If any answer has resolved your problem then you should mark that answer as right, so that can help other people, and this questions will be set as resolved, this is the way how this form works. thanks :) – Patriks Mar 13 '13 at 08:58
  • @Pratik You're right but the best here would be to close the question as duplicate (I voted to) unless OP explains why he needs to use `eval`. – Denys Séguret Mar 13 '13 at 09:00

7 Answers7

1

You should use JSON.parse() for that:

var parsedJSON = JSON.parse(JSONObj);
console.log(parsedJSON[0].orderStatus);  // "Pending"

Note: This method is supported by modern browsers only. Read about browser compatibility here.

VisioN
  • 143,310
  • 32
  • 282
  • 281
1
var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]';

var parsedJSON = JSON.parse(JSONObj);    
var result=parsedJSON.result;
var count=parsedJSON.count;
alert('result:'+result+' count:'+count);

However you do not have any JSON field called "result" or "count", so those would be undefined.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

You can use JSON.parse instead.

for browsers that does not support JSON.parse nativly(MSIE) you can use third party libraries like json2 or json3.

var parsedJSON = JSON.parse(JSONObj); 
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

You have a quote problem.

You might fix it by replacing

var parsedJSON = eval('(\'+JSONObj+\')');    

with

var parsedJSON = eval('('+JSONObj+')');    

But there is no reason here not to use JSON.parse.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0
var parsedJSON = eval(JSONObj);

this will give you parsed JSON object already. But there is no property name result within your Json object and so do count. And if you want to count the array then you just use length for e.g parsedJSON.length

FosterZ
  • 3,863
  • 6
  • 38
  • 62
0

try

var JSONObj='[{"rmaNumber":null,"orderReferenceNumber":"referene45","orderStatus":"Pending","dateRequested":null,"dateApproved":null,"segateAddress":null,"billingAddress":null,"shippingAddress":null,"returnForCredit":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"returnForExchange":{"requested":null,"received":null,"shipped":35,"credited":45,"invoiceAmount":null},"totals":null}]';

 eval("var  parsedJSON =" + JSONObj);
0

1st thing :

var parsedJSON = eval('(\'+JSONObj+\')');

should be

var parsedJSON = eval('('+JSONObj+')');   

2nd thing :
I don't see any result or count object is json string

3rd thing :
data in json is in array, so if any object result is there in objects, in array then it should be accessed with parsedJSON[0].result

Patriks
  • 1,012
  • 1
  • 9
  • 29