简体   繁体   中英

extract particular data from json file

I am new to using javascript as well as json. I need to extract certain sections from json for processing the data.

    "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"
  }

This is my json file. I need only 'body' from it.How to get this done using javascript? Please help me.

var jsonParsed = JSON.parse( put_yourJSON_here );

然后只使用jsonParsed.body

Provided that your JSON is a string and not just a general Javascript object, then you can access its properties by parsing it.

var jsonText = '{"id":"123", "name":"Joe Bloggs"}';
var jsonObj = JSON.parse(jsonText);

var name = jsonObj.name; // Joe Bloggs

Edit: Or you can get the resource using jQuery.getJSON():

$.getJSON( "path/to/file.json", function( data ) {
  var name = data.name; // Joe Bloggs
});

Youc an use JSON.parse to convert json data to javascript object and then get the data of body, try with below sample:

var data = JSON.parse("put your data here");
var body = data.body;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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