json字符串字段提取,在Talend 5.5中

时间:2014-07-10 11:50:08

标签: java json talend jsonpath

我有以下json字符串,我想从此字符串中提取"id"之后的所有"number":(15124,13498)字段。

我试图用java代码中的jsonpath这样做:

String pd = JsonPath.read(jsonString, "$.response.data.number.id");`

但是由于错误的jsonpath我得到了一个异常,我应该写一下jsonpath来获取"id"字段之后的"number"吗?

json String下面:

{
    "response": {
        "code": 200,
        "msg": "Success",
        "data": {
            "number": {
                "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 个答案:

答案 0 :(得分:0)

这很好用

 String pd = JsonPath.read("{\n" +
            "    \"response\": {\n" +
            "        \"code\": 200,\n" +
            "        \"msg\": \"Success\",\n" +
            "        \"data\": {\n" +
            "            \"number\": {\n" +
            "                \"id\": \"13498\",\n" +
            "                \"name\": \"(Not in used) Daniel - 30138444\",\n" +
            "                \"network_id\": 1,\n" +
            "                \"network_type\": \"Facebook\",\n" +
            "                \"currency\": \"USD\",\n" +
            "                \"currency_info\": {\n" +
            "                    \"prefix\": \"$\",\n" +
            "                    \"postfix\": \"\",\n" +
            "                    \"name\": \"US Dollars\"\n" +
            "                },\n" +
            "                \"timezone\": {\n" +
            "                    \"id\": 92,\n" +
            "                    \"code\": \"PST\",\n" +
            "                    \"region\": \"America\",\n" +
            "                    \"locality\": \"Los_Angeles\",\n" +
            "                    \"offset\": -7,\n" +
            "                    \"facebook_code\": 1\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "    }\n" +
            "}", "$.response.data.number.id");
    System.out.println(pd);

输出:

  13498

检查您是否在JSON

中传递了正确的jsonString

答案 1 :(得分:-1)

我认为,根据您正在寻找的内容,您将获得一个List作为输出。我还觉得将数据字段作为数组而不是对象更为明智。然后你可以尝试这样的事情;

List<String> ids = JsonPath.read(json, "$.response.data[*].number.id");