简体   繁体   English

如何使用 JavaScript 获得 url json 响应的特定行

[英]How to get a specific line of url json response with JavaScript

I don't know about JavaScript, but I need to use just a little part of it.我不知道 JavaScript,但我只需要使用它的一小部分。 I have a url "http://my-url/api/auth/v1" that generates me the following json response:我有一个 url "http://my-url/api/auth/v1" 生成以下 json 响应:

{
    "token": {
        "token": "token-result-here",
        "result": null,
        "resultCode": "0",
        "requestId": "xx"
    },

I need a script that returns me just the result of "token".我需要一个脚本,它只返回“令牌”的结果。 For example, when I call "token", it returns: "token-result-here".例如,当我调用“token”时,它返回:“token-result-here”。 Could anyone help me with this code?有人可以帮我处理这段代码吗?

If如果

var response = {
"token": {
    "token": "token-result-here",
    "result": null,
    "resultCode": "0",
    "requestId": "xx"
}};

then use response.token.token to access "token-result-here"然后使用response.token.token访问“token-result-here”

You can save the request to the api to a variable let's say "apiResponse" and you can do this:您可以将 api 的请求保存到变量中,比如说“apiResponse”,您可以这样做:

//parse the JSON
JSON.parse(apiResponse);
//access the JSON
let token = apiResponse.token.token;

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

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