[英]extract particular data from json file
我是使用javascript和json的新手。 我需要從json中提取某些部分以處理數據。
"response": {
"status": "OK",
"code": 200,
"header": [
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Pragma",
"value": "no-cache"
},
"body": "{\r\n \"@odata.context\":\"http://localhost:53292/odata/$metadata#Movies\",\"value\":[\r\n {\r\n \"Id\":1,\"Title\":\"StarWars - The Force Awakens\",\"ReleaseDate\":\"2015-10-25T00:00:00+05:30\",\"Rating\":\"FiveStar\",\"Director\":{\r\n \"FirstName\":\"J.J.\",\"LastName\":\"Abrams\"\r\n },\"LastModifiedOn\":\"2016-09-30T10:46:33.3114027+05:30\"\r\n },{\r\n \"Id\":2,\"Title\":\"Mad Max - The Fury Road\",\"ReleaseDate\":\"2015-05-15T00:00:00+05:30\",\"Rating\":\"FourStar\",\"Director\":{\r\n \"FirstName\":\"George\",\"LastName\":\"Miller\"\r\n },\"LastModifiedOn\":\"2016-09-30T10:46:33.3114027+05:30\"\r\n }\r\n ]\r\n}",
"cookie": [],
"responseTime": 30,
"responseSize": 583,
"update": {},
"reason": {},
"text": {},
"json": {},
"mime": {},
"dataURI": {},
"size": {},
"describe": {},
"toObjectResolved": {},
"toJSON": {},
"meta": {}
},
"id": "5a3d3fb3-93a7-4555-b0d5-a4482d98b888"
}
這是我的json文件。 我只需要它的“ body”。如何使用javascript完成此操作? 請幫我。
var jsonParsed = JSON.parse( put_yourJSON_here );
然后只使用jsonParsed.body
假設您的JSON是一個字符串,而不僅僅是一個普通的Javascript對象,那么您可以通過解析它來訪問其屬性。
var jsonText = '{"id":"123", "name":"Joe Bloggs"}';
var jsonObj = JSON.parse(jsonText);
var name = jsonObj.name; // Joe Bloggs
編輯:或者您可以使用jQuery.getJSON()獲取資源:
$.getJSON( "path/to/file.json", function( data ) {
var name = data.name; // Joe Bloggs
});
您可以使用JSON.parse
將json數據轉換為javascript對象,然后獲取正文的數據,請嘗試以下示例:
var data = JSON.parse("put your data here");
var body = data.body;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.