-1

When I use AJAX it will return the data:

 $.ajax({ 
        dataType: "json",
        url: mySQL ,     
        success: saveBarcode , 
        error: function ( xhr , b , c ) { 
            $("#reportMsg").html ( "箱號條碼存取失敗,建議重新執行!!" ) ;  },
        async: false });     // 解決工廠資料會不正確問題

  function saveBarcode ( json ) { 
alert(JSON.stringify(json.KB_MOBILE_PHY_READ('PS2-P2100','P2100','3949791')));

 }


the json data will contain [{"KB_MOBILE_PHY_READ('PS2-P2100','P2100','421221')":"3"}]

How do I get the number 3?

I use:

json.KB_MOBILE_PHY_READ('PS2-P2100','P2100','421221') 

but it won't work

子維 宋
  • 85
  • 1
  • 11
  • 1
    Please post some of your code. This question is pretty much impossible to answer in its current state. – Nathan Sep 25 '14 at 03:12
  • 2
    You can refer to values on objects in JavaScript either through *dot notation* (`x.y`) or *indexing notation* (`x["y"]`). If your object's key contains characters which aren't legal in an identifier, such as `'` and `,` here, you have to use indexing. – Chris Hayes Sep 25 '14 at 03:15
  • Damn @ChrisHayes, too fast :P – Centril Sep 25 '14 at 03:15
  • Since `json.KB_MOBILE_PHY_READ('PS2-P2100','P2100','343')` doesn't even match what's in your JSON, I'm not surprised it doesn't work. – Matt Burland Sep 25 '14 at 03:15
  • 1
    Also: [How to access object properties containing special characters?](http://stackoverflow.com/q/12953704/218196) (actually I should have closed it as a duplicate of that one I guess). – Felix Kling Sep 25 '14 at 03:24

1 Answers1

2
alert(obj[0]["KB_MOBILE_PHY_READ('PS2-P2100','P2100','421221')"]);

[0] is because you have array of objects

ps: as it is written in comments to the question - properties of the object can be accessed the same way as in the case of associative arrays, i.e. x.y is the same as x['y'] or x["y"].

Cheery
  • 16,063
  • 42
  • 57
  • Please include some explanation as to why the questioners attempt failed like @Chris Hayes has provided. – Centril Sep 25 '14 at 03:17
  • @Centril I think it is obvious from the example. – Cheery Sep 25 '14 at 03:18
  • 1
    If it was obvious, the questioner wouldn't have made the mistake... If it is OK with Chris Hayes, you should copy paste what he wrote into your answer to make it more complete. – Centril Sep 25 '14 at 03:20
  • I chose not to write an answer myself because I expected the question to quickly get closed. Now that it's happened, I think the point is largely moot. – Chris Hayes Sep 25 '14 at 03:33