简体   繁体   中英

Show Json Object In TextBox

I have json returned from Database.I want to pick only one object Value and show it in the textbox. Here is my json.

[{
  "ErrorMessage":"",
  "ID":294,
  "ExpenseID":0,
  "EffectiveDate":"/Date(1262284200000)/",
  "FormattedEffectiveDate":"01-01-2010",
  "Perunit":null,
  "VATRate":17.5,
  "ChangedByID":1,
  "ChangedByName":"superuser, superuser",
  "Expense":null,
  "ErrorSummary":null,
  "ErrorList":[]
 }]  

I have Tried

 var Jsoninvoice = JSON.stringify(data)
 alert(Jsoninvoice.VATRate) and also alert(data.VATRate)

Thank you In advance.

You have an array containing 1 object. stringify turns this object into a string - you need it parsed so you can use it.

(I'm not sure if the object is parsed already, so to cover all bases, we'll parse it)

var Jsoninvoice = JSON.parse(data);
alert(Jsoninvoice[0].VATRate);

You have to specify the arrays index before you can access the properties.

It is already json object and stringify is not needed as @tymJV said you need to parse it if it is returned as string, just you need to access array item, as it is an array:

alert(data[0].VATRate)

SEE FIDDLE

You could use $.parseJSON(YOURJSON) , and then use the keys to pull the data. Since it's in an array, you'll have to use [0] to pull the first item in the array (ie: your data).

Example

$(document).ready(function(){
    var j ='[{"ErrorMessage":"","ID":294,"ExpenseID":0,"EffectiveDate":"/Date(1262284200000)/","FormattedEffectiveDate":"01-01-2010","Perunit":null,"VATRate":17.5,"ChangedByID":1,"ChangedByName":"superuser, superuser","Expense":null,"ErrorSummary":null,"ErrorList":[]}]';
    var json = $.parseJSON(j);
    alert("VATRate: "+json[0].VATRate);
});

Fiddle for reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM