如何查询一个json文件?

Abd*_*rim 4 javascript xpath json jsonpath typescript

这是我想解析的文件

  1. 我从 JSON 格式的网络服务接收文件。
  2. 我想以显示美国总统姓名的方式解析内容
{
    "response": {
        "result": {
            "Countries": {
                "row": [
                    {
                        "no": "1",
                        "FL": [
                            {
                                "content": "USA",
                                "val": "Country"
                            },
                            {
                                "content": "Barack Obama",
                                "val": "President"
                            }
                        ]
                    },
                    {
                        "no": "2",
                        "FL": [
                            {
                                "content": "Cuba",
                                "val": "Country"
                            },
                            {
                                "content": "Raul Castro",
                                "val": "President"
                            }
                        ]
                    }
                ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

预期输出

{ presidents: [
    { "name": "Barack Obama"}
    ]
}
Run Code Online (Sandbox Code Playgroud)

你能提供一个使用一种 JSON XPath 的解决方案吗?

Emi*_*sen 6

假设您正在将响应加载到变量数据中:

var data = {
    "response" : {
        "result" : {
            "Countries" : {
                "row" : [{
                        "no" : "1",
                        "FL" : [{
                                "content" : "USA",
                                "val" : "Country"
                            }, {
                                "content" : "Barack Obama",
                                "val" : "President"
                            }
                        ]
                    }, {
                        "no" : "2",
                        "FL" : [{
                                "content" : "Cuba",
                                "val" : "Country"
                            }, {
                                "content" : "Raul Castro",
                                "val" : "President"
                            }
                        ]
                    }
                ]
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样过滤数据:

data.response.result.Countries.row.filter(function (el) {
    return (el.FL[0].content == "USA");
})[0].FL[1];
Run Code Online (Sandbox Code Playgroud)

到达,得到:

{
    "content" : "Barack Obama",
    "val" : "President"
}
Run Code Online (Sandbox Code Playgroud)

要获取名称,只需指定“内容”

data.response.result.Countries.row.filter(function(el){
  return (el.FL[0].content == "USA");
})[0].FL[1].content;
Run Code Online (Sandbox Code Playgroud)

编辑 1

可以像字符串一样搜索 json 对象。

如果我们知道元素没有子元素,那么我们可以使用这样的方法:

function find(query,obj) {
  var str = JSON.stringify(obj);
  var start = str.substr(0,str.indexOf(query)).lastIndexOf('{');
  var end = str.substr(start,str.length).indexOf('}');
  return str.substr(start,end);
}

console.log(find('"content":"USA"',data))
Run Code Online (Sandbox Code Playgroud)