0

我有如下 JSON 字符串:

{
    "ConfigurationJsonResult":
    [
        2246,
        2247,
        2248,
        2249,
        2250,
        2251,
        2252,
        2253,
        2254,
        2255
    ]
}

我的问题是我想使用 xPath 获得第一个值,即 2246。

我试过使用/ConfigurationJsonResult[1]但它给了我[2246,2247.....2255],我只想要 2246。如何实现它。

4

1 回答 1

1

您可以编写一个映射函数将 xpath 转换为 jsonpath 并使用转换后的字符串来访问数据。

function xpathToJsonPath(xpath) {
   var jsonPath = xpath.replace("//","..");
   //Simillarly other mappings
   return jsonPath;
}

var data = jsonPath(object, xpathToJsonPath("$..author"));

映射给出了您共享的链接 - http://goessner.net/articles/JsonPath/

您可以对此进行大量改进,例如将其作为函数扩展到原型等。

于 2013-12-05T06:48:23.010 回答