如何查询json文件?

时间:2016-04-12 11:10:40

标签: javascript json xpath typescript jsonpath

这是我要解析的文件

  1. 我从JSON格式的网络服务接收文件。
  2. 我想以这样的方式解析内容,以便显示来自美国的总统姓名。
  3. {
        "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"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
    

    预期输出

    { presidents: [
        { "name": "Barack Obama"}
        ]
    }
    

    您可以使用一种JSON XPath提供解决方案吗?

3 个答案:

答案 0 :(得分:3)

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

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

然后您可以按照以下方式过滤数据:

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

到达:

{
    "content" : "Barack Obama",
    "val" : "President"
}

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

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

编辑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))

答案 1 :(得分:2)

t = {
    "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"
                            }
                        ]
                    }
                ]
            }
        }
    }
}
res={};//Here we will store result
for (i in t.response.result.Countries.row) {
  // get current country
  country = t.response.result.Countries.row[i].FL[0].content;
  // get current president
  president = t.response.result.Countries.row[i].FL[1].content;
  if (country == 'USA') {
    res.presidents=[{name:president}];
    break;
  }
}

答案 2 :(得分:0)

尽管存在问题的年龄,但我想将此答案添加为有相同问题的未来访客的参考:

您可以使用JSONPath。该页面包含JavaScript和PHP的描述和实现。