如何在JSON.stringify()之后获取特定值

时间:2018-02-08 07:43:35

标签: javascript

这是在console.log中显示的数据。

{"data":
    [
        {
            "CloserName":null,
            "agent_id":"10807",
            "AgentName":"TEST",
            "SurveyDate":"02/02/2018 02:18:46 AM",
            "SurveyName":"Ruth  ",
            "state":"West Bengal",
            "phone":"9836969715",
            "status":"Approved",
            "verification_progress":"Pending",
            "survey_id":"1",
            "rejection_remarks":"aa",
            "tl_remarks":"Pending"
        }
    ],
    "count":1
}

任何人都可以帮我显示一个值(即survey_id)吗?我只想获取survey_id

4 个答案:

答案 0 :(得分:3)

这是一个例子:

var json = {  
   "data":[  
      {  
         "CloserName":null,
         "agent_id":"10807",
         "AgentName":"TEST",
         "SurveyDate":"02/02/2018 02:18:46 AM",
         "SurveyName":"Ruth ",
         "state":"West Bengal",
         "phone":"9836969715",
         "status":"Approved",
         "verification_progress":"Pending",
         "survey_id":"1",
         "rejection_remarks":"aa",
         "tl_remarks":"Pending"
      }
   ],
   "count":1
}

// get first id
var id = json.data[0].survey_id
console.log(id)

// get all ids
var ids = json.data.map(x => x.survey_id)
console.log(ids)

如果JSON是字符串化的,请先调用JSON.parse(jsonStr)

答案 1 :(得分:1)

您必须将JSON-String解析为对象。之后,您可以使用默认对象标识符访问数据。

const object = JSON.parse('{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}');
console.log(object.data[0].survey_id)

答案 2 :(得分:0)

如果您的JSON数据已字符串化(您的示例JSON是有效的JSON对象,而不是字符串),您首先需要解析它,然后获取ID(假设您在数据数组中有多个项目)并将其注销。有几种不同的方法可以实现这一目标:



const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
    
let parsed = JSON.parse(stringified);
    
parsed = parsed.data.map(item => item.survey_id);
console.log(parsed);




您也可以循环遍历数组中的项目,并使用for循环逐个记录它们:



const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
    
let parsed = JSON.parse(stringified);
    
for (let i = 0; i < parsed.data.length; i++) {
    console.log(parsed.data[i].survey_id);
}
&#13;
&#13;
&#13;

或使用for-of:

&#13;
&#13;
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';

let parsed = JSON.parse(stringified);

for (const item of parsed.data) {
    console.log(item.survey_id);
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

根据您当前的对象结构

你的对象

public void genericMethod(Cat t)

获取survey_id

Cat