【问题标题】:jsonpath using regular expressions in Talend 5.5在 Talend 5.5 中使用正则表达式的 jsonpath
【发布时间】:2014-07-16 14:47:51
【问题描述】:

我有这个 json 字符串:

我想提取数字节点之后的所有id:"0","1","2"...等。

我通过使用 jsonpath 成功获得了一个 id:$.response.data.0.id 并获得了"15124"。 但我正在寻找一个 jsonpath,它将提取 Json 字符串中的所有 id。 换句话说,这是扩展输出:15124,13498,14296,13364,14060,13672

这是我拥有的 Json 字符串:

{
"response": {
    "code": 200,
    "msg": "Success",
    "data": {
        "0": {
            "id": "15124",
            "name": " yoav (yoavshaki@yahoo.com) - 301519506662355",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 139,
                "code": "IST",
                "region": "Asia",
                "locality": "Jerusalem",
                "offset": 3,
                "facebook_code": 70
            }
        },
        "1": {
            "id": "13498",
            "name": "(Not in used) Daniel - 30138444",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 92,
                "code": "PST",
                "region": "America",
                "locality": "Los_Angeles",
                "offset": -7,
                "facebook_code": 1
            }
        },
        "2": {
            "id": "14296",
            "name": "Daniel - ComeOn (bingocafe@walla.com - 1375713835981039)",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 92,
                "code": "PST",
                "region": "America",
                "locality": "Los_Angeles",
                "offset": -7,
                "facebook_code": 1
            }
        },
        "3": {
            "id": "13364",
            "name": "Erez - 116060088528093",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 92,
                "code": "PST",
                "region": "America",
                "locality": "Los_Angeles",
                "offset": -7,
                "facebook_code": 1
            }
        },
        "4": {
            "id": "14060",
            "name": "Erez - NordicBet (gianniciano82@gmail.com - 105134566315107)",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 139,
                "code": "IST",
                "region": "Asia",
                "locality": "Jerusalem",
                "offset": 3,
                "facebook_code": 70
            }
        },
        "5": {
            "id": "13672",
            "name": "Erez - alon.dan - 1378526859026272",
            "network_id": 1,
            "network_type": "Facebook",
            "currency": "USD",
            "currency_info": {
                "prefix": "$",
                "postfix": "",
                "name": "US Dollars"
            },
            "timezone": {
                "id": 92,
                "code": "PST",
                "region": "America",
                "locality": "Los_Angeles",
                "offset": -7,
                "facebook_code": 1
            }
        }
    }
}
}

感谢所有的帮助!

【问题讨论】:

  • 你试过$.response.data.*.id吗?
  • @nerdwaller 不,这是 Java 库 jsonpath 的语法。
  • 你已经尝试过......到目前为止,什么?请不要来这里让别人为你写代码。
  • $.response.data.*.id 可以工作。

标签: java regex json talend jsonpath


【解决方案1】:

GSON library 是将 java 对象转换为 json 字符串的好选择,反之亦然。

这里是使用 [Gson#fromJson()] 将 JSON 字符串转换为 java Map 的示例代码。

Find more examples...

示例代码:

BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json.txt")));
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Map<String, Object>>>() {}.getType();
Map<String, Map<String, Object>> map = gson.fromJson(reader, type);

Map<String, Map<String, Object>> innerMap = (Map<String, Map<String, Object>>) map.get("response").get("data");
for (String key : innerMap.keySet()) {
    System.out.println("key:" + key + " id:" + innerMap.get(key).get("id"));
}

输出:

key:0 id:15124
key:1 id:13498
key:2 id:14296
key:3 id:13364
key:4 id:14060
key:5 id:13672

【讨论】:

    【解决方案2】:

    感谢 Syam S. 的回答。

    $.response.data.*.id 确实有效!

    【讨论】:

    • 您应该感谢 cmets 中的用户,而不是发布新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多