如何使用javascript / jquery访问json数组的元素?

时间:2016-12-23 07:37:16

标签: javascript jquery arrays json

我有一个像这样的json数组

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的值并在alert中显示它。我是jquery / javascript的新手,所以任何帮助都会受到赞赏。

7 个答案:

答案 0 :(得分:2)

您可以通过点表示法在js中像普通对象一样访问json。

var json = {"STOCKQTY":2.0,"PHYSICALQTY":1.0};

console.log(json.PHYSICALQTY);

如果你把这个json保存在另一个对象下你需要更深入,例如。

var response = {AlreadySaved: [{"STOCKQTY":2.0,"PHYSICALQTY":1.0}] };

console.log(response.AlreadySaved[0].PHYSICALQTY);

请记住,在某些情况下,您可能将json(例如来自响应)视为字符串而非对象。

console.log(typeof someJson); //string

在这种情况下,您需要将此字符串解析为json

var json = JSON.parse(someJsonAsString);

快乐的黑客攻击!

答案 1 :(得分:1)

您可以使用 private void Form1_Load(object sender, EventArgs e) { foreach (Control c in this.Controls) { btn = c as Button; { if (btn == null) continue; c.MouseWheel += c_MouseWheel; } } } private void c_MouseWheel(object sender, MouseEventArgs e) { TabControl tabControl = sender as TabControl; if (tabControl != null) { if (e.Delta < 0) { tabControl.Size = new Size(-2, -2); } else { tabControl.Size = new Size(+2, +2); }

json[0].PHYSICALQTY
var json = [{
  "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
}];

document.getElementById('id1').innerHTML=json[0].PHYSICALQTY;

答案 2 :(得分:0)

这不是json对象。

JSON:

foreach($entities as entity)
{
    $em->flush();
}

在这种情况下,您可以获得您的财产: 警报(AlreadySaved [0] .PHYSICALQTY);

答案 3 :(得分:0)

您可以解析它,然后获取值。当然,如果你收到这个就像一个字符串。

2-27 17:34:32.461 4910-4910/com.crm.mail I/MCOperationQueue.cpp: trying to quit 0xb4a07c00
12-27 17:34:32.462 4910-4934/com.crm.mail I/MCOperationQueue.cpp: stopping 0xb4a07c00
12-27 17:34:32.462 4910-4910/com.crm.mail I/MCOperationQueue.cpp: thread stopped 0xb4a07c00
12-27 17:34:32.462 4910-4934/com.crm.mail I/MCOperationQueue.cpp: cleanup thread 0xb4a07c00

答案 4 :(得分:0)

您的JSON对象无效,您可以在http://jsonlint.com/

查看

将其设置为

之后
var 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}] ;

您可以通过AlreadySaved[0]["PHYSICALQTY"]

访问它

答案 5 :(得分:0)

首先你必须像这样序列化你的对象;

var json_data = JSON.parse(AlreadySaved);

如果您知道json_data中的特定数据,则可以使用索引编号进行调用;

var result= json_data[0]["PHYSICALQTY"];

或者你可以找到你想要的任何东西,你可以找到foreach;

for(var item in json_data) {
   alert(item["PHYSICALQTY"].value);
}

答案 6 :(得分:0)

请试试这个:

//In case you are getting this from ajax request put "returnObject.Json_Variable" instead of "Json_Variable"
var obj = (JSON.parse("Json_Variable"));
var resultPHYSICALQTY=obj.PHYSICALQTY);