如何使用javascript/jquery访问json数组的元素? [英] How to access elements of json array using javascript/jquery?
本文介绍了如何使用javascript/jquery访问json数组的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的json数组
I have a json array like this
AlreadySaved:[
{
"SNO":1,
"SNUMBER":"827",
"STARTDATE":"11/12/2016",
"STARTTIME":"03:06:50 PM",
"ITEMNAME":"KBand",
"ITEMCODE":"KB2541",
"ITEMSKUNAME":"Band",
"ITEMSKUCODE":"BT102",
"SALESRATE":100.0,
"PURCHASERATE":5.0,
"DOE":"~",
"STOCKQTY":2.0,
"PHYSICALQTY":1.0
}
]
我需要获取PHYSICALQTY的值并将其显示在警报中.我是jquery/javascript的新手,所以将不胜感激.
I need to fetch value of PHYSICALQTY and display it in alert.I am new to jquery/javascript so any help would be appreciated.
推荐答案
您可以通过点表示法像js中的普通对象一样访问json.
You can access json like normal objects in js via dot notation.
var json = {"STOCKQTY":2.0,"PHYSICALQTY":1.0};
console.log(json.PHYSICALQTY);
如果您已将此json保存在另一个对象下,则需要更深入地研究,例如.
If you Have saved this json under another object You need to go deeper, eg.
var response = {AlreadySaved: [{"STOCKQTY":2.0,"PHYSICALQTY":1.0}] };
console.log(response.AlreadySaved[0].PHYSICALQTY);
还请记住,在某些情况下,您可能将json(例如,来自响应)作为字符串而不是对象.
Please remember also that in some cases You may have json (eg from response) as string not object.
console.log(typeof someJson); //string
在这种情况下,您需要将此字符串解析为json
In that cases You need to parse this string into json
var json = JSON.parse(someJsonAsString);
黑客很开心!
这篇关于如何使用javascript/jquery访问json数组的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文