2016-02-27 18 views
0

这是我的拉动脚本的JSON文件... 我不知道如何拉分数部分,并尝试了多种方式... 此方法的工作原理拉满分部分 - 我的ID和甚至评级节后...JSON文件查询错误评分从评分json文件

var link = jsonEscape((data["requests"][0]["response_body"])); 

这种方法行不通......

var link = jsonEscape((data["requests"][0]["response_body"]["scores"][0]["id"])); 

我在做什么错了.. 。 找到下面我的完整代码....

​​

这是我的JSON文件...

{ 
    "resource": "Scores", 
    "http_method": "GET", 
    "route": "/scores", 
    "description": "List all scores", 
    "explanation": null, 
    "parameters": [ 
    { 
     "name": "page", 
     "description": "Fetch the nth page" 
    }, 
    { 
     "name": "per_page", 
     "description": "Number of objects per page" 
    }, 
    { 
     "name": "offset", 
     "description": "Pad a number of records" 
    } 
    ], 
    "response_fields": [ 

    ], 
    "requests": [ 
    { 
     "request_method": "GET", 
     "request_path": "/scores", 
     "request_body": null, 
     "request_headers": { 
     "Accept": "application/json", 
     "Content-Type": "application/json", 
     "Host": "example.org", 
     "Cookie": "" 
     }, 
     "request_query_parameters": { 

     }, 
     "request_content_type": "application/json", 
     "response_status": 200, 
     "response_status_text": "OK", 
     "response_body": "{\n \"scores\": [\n {\n  \"id\": 1,\n  \"value\": 3,\n  \"subject\": {\n  \"id\": 1,\n  \"fullname\": \"Elza Greenfelder\",\n  \"email\": \"[email protected]\",\n  \"address\": \"Teufenerstrasse\",\n  \"latitude\": null,\n  \"longitude\": null\n  },\n  \"topic\": {\n  \"id\": 1,\n  \"name\": \"Tag Case\"\n  },\n  \"ratings\": [\n  {\n   \"id\": 1,\n   \"value\": 1\n  },\n  {\n   \"id\": 2,\n   \"value\": 1\n  },\n  {\n   \"id\": 3,\n   \"value\": 2\n  }\n  ]\n },\n {\n  \"id\": 2,\n  \"value\": 3,\n  \"subject\": {\n  \"id\": 8,\n  \"fullname\": \"Raven Witting\",\n  \"email\": \"[email protected]\",\n  \"address\": \"Durachweg\",\n  \"latitude\": null,\n  \"longitude\": null\n  },\n  \"topic\": {\n  \"id\": 5,\n  \"name\": \"Tag Filter\"\n  },\n  \"ratings\": [\n  {\n   \"id\": 4,\n   \"value\": 2\n  },\n  {\n   \"id\": 5,\n   \"value\": 1\n  },\n  {\n   \"id\": 6,\n   \"value\": 3\n  }\n  ]\n },\n {\n  \"id\": 3,\n  \"value\": 2,\n  \"subject\": {\n  \"id\": 15,\n  \"fullname\": \"Cristopher Buckridge\",\n  \"email\": \"tamia_[email protected]\",\n  \"address\": \"Teufenerstrasse\",\n  \"latitude\": null,\n  \"longitude\": null\n  },\n  \"topic\": {\n  \"id\": 9,\n  \"name\": \"Digital Viewer\"\n  },\n  \"ratings\": [\n  {\n   \"id\": 7,\n   \"value\": 1\n  },\n  {\n   \"id\": 8,\n   \"value\": 2\n  },\n  {\n   \"id\": 9,\n   \"value\": 3\n  }\n  ]\n }\n ]\n}", 
     "response_headers": { 
     "X-Frame-Options": "SAMEORIGIN", 
     "X-XSS-Protection": "1; mode=block", 
     "X-Content-Type-Options": "nosniff", 
     "X-Total": "3", 
     "X-Total-Pages": "1", 
     "X-Per-Page": "25", 
     "X-Page": "1", 
     "X-Next-Page": "", 
     "X-Prev-Page": "", 
     "X-Offset": "", 
     "Content-Type": "application/json; charset=utf-8", 
     "ETag": "W/\"f1efdc568cbd238621cce62d52455417\"", 
     "Cache-Control": "max-age=0, private, must-revalidate", 
     "X-Request-Id": "1dfccc08-e28e-4068-b9a2-7d11be431877", 
     "X-Runtime": "0.053501", 
     "Vary": "Origin", 
     "X-Rack-CORS": "preflight-hit; no-origin", 
     "Content-Length": "822" 
     }, 
     "response_content_type": "application/json; charset=utf-8", 
     "curl": "curl \"http://localhost:3000/scores\" -X GET \\\n\t-H \"Accept: application/json\" \\\n\t-H \"Content-Type: application/json\" \\\n\t-H \"Host: example.org\" \\\n\t-H \"Cookie: \"" 
    } 
    ] 
} 
+0

这是怎么JSON文件生成在第一个地方?看起来很奇怪,在其中包含所有这些标题。这就好像另一个请求正在进行,而不是分析响应 – charlietfl

回答

0

您可以使用JSON.parse解析response_body成JSON:

$.getJSON(link_name, 
    function(data){ 
     var body= JSON.parse(data["requests"][0]["response_body"]); 
     var id = body["scores"][0]["id"]; 
    } 
); 

例如:https://jsbin.com/bewopenoqo/edit?js,console

+0

感谢你正在工作... – Dean