简体   繁体   English

如何获取json格式字符串(从表中提取)的值

[英]How to get json format string(extract from table)'s value

I have extract a json format data from a table like this: 我从这样的表中提取了json格式的数据:

[ { id:"3",
"Date Extracted":"12/3",
Experiment:"normal"},
{ id:"2",
"Date Extracted":"12/3",
Experiment:"powder",
Notes:"" },
{ id:"1",
"Date Extracted":"12/3",
Experiment:"fine",
Notes:"" }
]

And I have used function like below to get only the attributes of each id like "12/3","normal", not include the name like "Date Extracted", Experiments. 而且我已经使用如下函数来获取每个ID的属性,例如“ 12/3”,“普通”,而不包括名称“实验提取的日期”。 Somehow this does not work. 不知何故,这行不通。 but I have no idea how do it exactly in details. 但我不知道具体如何做。

var j = JSON.parse(data);

function(){
for(name in j.property.name)
console.log(j.property.value);
}

The extracted json data had some validation issues with the property names. 提取的json数据在属性名称方面存在一些验证问题。 You could verify your jsondata via jsonlint.com . 您可以通过jsonlint.com验证jsondata

So the json data should likely be: 因此json数据可能应该是:

var jsonData ='[{ "id":"3", "Date Extracted":"12/3","Experiment":"normal"}, { "id":"2","Date Extracted":"12/3","Experiment":"powder","Notes":"" },{ "id":"1","Date Extracted":"12/3","Experiment":"fine","Notes":"" }]';

Function: 功能:

var json = $.parseJSON(jsonData);
$(json).each(function(i,val){
    $.each(val,function(name,value){
        console.log(name + " : " + value);       
    });
});

You can't use "for" like that. 您不能这样使用“ for”。

var j = JSON.parse('[{  "id": "3",  "Date Extracted": "12/3",   "Experiment": "normal"}, {  "id": "2",  "Date Extracted": "12/3",   "Experiment": "powder", "Notes": ""}, { "id": "1",  "Date Extracted": "12/3",   "Experiment": "fine",   "Notes": ""}]');for(var i=0;i<j.length;i++){
var obj = j[i];
for(var key in obj){
    var attrName = key;
    var attrValue = obj[key];
    console.log(attrValue);
}}

 var data = [{ id: "3", "Date Extracted": "12/3", Experiment: "normal" }, { id: "2", "Date Extracted": "12/3", Experiment: "powder", Notes: "" }, { id: "1", "Date Extracted": "12/3", Experiment: "fine", Notes: "" }]; for (var i = 0; i < data.length; i++) { console.log("Date Extracted " + data[i]['Date Extracted'])//for index Date Extracted console.log("Experiment " + data[i]['Experiment'])//for index Experiment } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

You can do using for loop like this 你可以像这样使用for循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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