简体   繁体   English

如何从JavaScript中的自定义json字符串中提取值?

[英]How to extract values from a custom json string in javascript?

How do I extract values from the below mentioned JSON using java script? 如何使用Java脚本从下面提到的JSON中提取值?

var json = {
  "headers": {
    "Access-Control-Allow-Headers": "origin",
    "Access-Control-Allow-Methods": "GET",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Max-Age": "3628800",
    "Connection": "keep-alive",
    "Content-Length": "981",
    "Content-Type": "application/json",
    "Date": "Thu, 12 Jan 2017 15:52:27 GMT",
    "Server": "Apigee LB"
  },
  "content": "{\n  \"apiProducts\" : [ ],\n  \"appFamily\" : \"default\",\n  \"appId\" : \"a937ece1-a0be-4475-bfea-e11fdeb279c6\",\n  \"attributes\" : [ {\n    \"name\" : \"DisplayName\",\n    \"value\" : \"CustomerApp\"\n  }, {\n    \"name\" : \"Notes\",\n    \"value\" : \"\"\n  }, {\n    \"name\" : \"fusion.expiry.date\",\n    \"value\" : \"2/10/2017\"\n  } ],\n  \"callbackUrl\" : \"\",\n  \"createdAt\" : 1484029734897,\n  \"createdBy\" : \"suvojitc@rssoftware.co.in\",\n  \"credentials\" : [ {\n    \"apiProducts\" : [ {\n      \"apiproduct\" : \"Customer and Product\",\n      \"status\" : \"revoked\"\n    } ],\n    \"attributes\" : [ ],\n    \"consumerKey\" : \"wNy0oKRhisvtMpjCR3dp67vZAC5lV3NV\",\n    \"consumerSecret\" : \"mTiGGgHoAAGrqWmh\",\n    \"expiresAt\" : 1486621737213,\n    \"issuedAt\" : 1484029737213,\n    \"scopes\" : [ ],\n    \"status\" : \"approved\"\n  } ],\n  \"developerId\" : \"793e585c-f093-41d6-88e9-e235bfd087ed\",\n  \"lastModifiedAt\" : 1484049963233,\n  \"lastModifiedBy\" : \"suvojitc@rssoftware.co.in\",\n  \"name\" : \"CustomerApp\",\n  \"scopes\" : [ ],\n  \"status\" : \"approved\"\n}",
  "status": {
    "message": "OK",
    "code": "200"
  }
}

Please Help. 请帮忙。

Json is made for Javascript. Json专为Javascript而设计。 All you should need to do is json.headers.Connection , which should retrieve keep-alive . 您需要做的就是json.headers.Connection ,它应该检索keep-alive

Rinse and repeat. 冲洗并重复。 Otherwise, get a parsing library. 否则,获取一个解析库。

The exception is with non-regular symbols in variable names. 变量名称中的非常规符号除外。 Note that dot notation will not accept Access-Control-Allow-Headers , instead it will throw an error. 请注意,点表示法将不接受Access-Control-Allow-Headers ,而是将引发错误。 To get around this, use the box notation. 要解决此问题,请使用方框符号。

json.headers["Access-Control-Allow-Headers"] which will retrieve origin json.headers["Access-Control-Allow-Headers"]将检索origin

The box notation takes in strings, so you must have the quote around Access-Control-Allow-Headers . 框符号使用字符串,因此您必须在Access-Control-Allow-Headers周围加上引号。

json.headers[Access-Control-Allow-Headers] will also throw a syntax error. json.headers[Access-Control-Allow-Headers]也会引发语法错误。

Edit: As Mike C rightly points out, the '.' 编辑:正如Mike C正确指出的那样,“。” notation will not accept the '-' symbol. 表示法将不接受“-”符号。

You can try like this. 您可以这样尝试。 As first you need to convert it to json string and then convert it to json object so that you can fetch the value with required key. 首先,您需要将其转换为json字符串,然后将其转换为json对象,以便您可以使用必需的键来获取值。

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> </title> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> </head> <body> <script> var json={ "headers": { "Access-Control-Allow-Headers": "origin", "Access-Control-Allow-Methods": "GET", "Access-Control-Allow-Origin": "*", "Access-Control-Max-Age": "3628800", "Connection": "keep-alive", "Content-Length": "981", "Content-Type": "application/json", "Date": "Thu, 12 Jan 2017 15:52:27 GMT", "Server": "Apigee LB" }, "content": "{\\n \\"apiProducts\\" : [ ],\\n \\"appFamily\\" : \\"default\\",\\n \\"appId\\" : \\"a937ece1-a0be-4475-bfea-e11fdeb279c6\\",\\n \\"attributes\\" : [ {\\n \\"name\\" : \\"DisplayName\\",\\n \\"value\\" : \\"CustomerApp\\"\\n }, {\\n \\"name\\" : \\"Notes\\",\\n \\"value\\" : \\"\\"\\n }, {\\n \\"name\\" : \\"fusion.expiry.date\\",\\n \\"value\\" : \\"2/10/2017\\"\\n } ],\\n \\"callbackUrl\\" : \\"\\",\\n \\"createdAt\\" : 1484029734897,\\n \\"createdBy\\" : \\"suvojitc@rssoftware.co.in\\",\\n \\"credentials\\" : [ {\\n \\"apiProducts\\" : [ {\\n \\"apiproduct\\" : \\"Customer and Product\\",\\n \\"status\\" : \\"revoked\\"\\n } ],\\n \\"attributes\\" : [ ],\\n \\"consumerKey\\" : \\"wNy0oKRhisvtMpjCR3dp67vZAC5lV3NV\\",\\n \\"consumerSecret\\" : \\"mTiGGgHoAAGrqWmh\\",\\n \\"expiresAt\\" : 1486621737213,\\n \\"issuedAt\\" : 1484029737213,\\n \\"scopes\\" : [ ],\\n \\"status\\" : \\"approved\\"\\n } ],\\n \\"developerId\\" : \\"793e585c-f093-41d6-88e9-e235bfd087ed\\",\\n \\"lastModifiedAt\\" : 1484049963233,\\n \\"lastModifiedBy\\" : \\"suvojitc@rssoftware.co.in\\",\\n \\"name\\" : \\"CustomerApp\\",\\n \\"scopes\\" : [ ],\\n \\"status\\" : \\"approved\\"\\n}", "status": { "message": "OK", "code": "200" } }; var str = JSON.stringify(json); var obj = JSON.parse(str); console.log(obj.headers["Access-Control-Allow-Headers"]); console.log(obj.headers["Access-Control-Allow-Methods"]); </script> </body> </html> 

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

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