使用java提取引号括起的特定值

时间:2016-10-13 13:03:34

标签: java json talend jira-rest-api jsonpath

我正在阅读JSON格式的REST API。在阅读JSON时,我无法使用JSONPath提取叶子。所以我想做的是通过JSON字符串运行Java并获取我需要的值,它们总是以相同的顺序。这是我需要从以下内容中提取值的JSON字符串:

{
"10516": {
    "estimated": {
        "value": 10.0,
        "text": "10.0"
    },
    "completed": {
        "value": 7.5,
        "text": "7.5"
    }
},
"10244": {
    "estimated": {
        "value": 15.5,
        "text": "15.5"
    },
    "completed": {
        "value": 7.5,
        "text": "7.5"
    }
},
"10182": {
    "estimated": {
        "value": 12.0,
        "text": "12.0"
    },
    "completed": {
        "value": 10.0,
        "text": "10.0"
    }
},
"10391": {
    "estimated": {
        "value": 16.0,
        "text": "16.0"
    },
    "completed": {
        "value": 3.0,
        "text": "3.0"
    }
},
"10183": {
    "estimated": {
        "value": 12.0,
        "text": "12.0"
    },
    "completed": {
        "value": 7.0,
        "text": "7.0"
    }
},
"10123": {
    "estimated": {
        "value": 11.5,
        "text": "11.5"
    },
    "completed": {
        "value": 5.5,
        "text": "5.5"
    }
},
"10447": {
    "estimated": {
        "value": 7.0,
        "text": "7.0"
    },
    "completed": {
        "value": 3.0,
        "text": "3.0"
    }
}}

正如您可以看到ID 10516包含estimatedcompleted,我想为每个ID提取这些值。所以输出应该如下所示:

ID    | ESTIMATED  |  COMPLETED
10516 | 10.0       |  7.5
10244 | 15.5       |  7.5
10182 | 12.0       |  10.0

依旧......

重要的是,对于每个ID,值都在同一行,因为我需要将它们上传到我的PostgreSQL数据库,并将此数据与ID上的其他数据连接。

我的想法是确定引号所包围的值,得到第1,第5,第9,第10,第14和第18个值,依此类推。因为JSON字符串总是以相同的顺序,我猜它可以这样做..

请大家帮帮忙,请记住,我根本没有任何java经验,实施工作将在Talend Open Studio中完成。还看到我提出这个问题的另一个问题,因为JSONPath无法帮助我:Extract leaves from JSON file with JSONpath

2 个答案:

答案 0 :(得分:0)

  

什么JSON解析器将帮助我提取叶子(10516,10244等)?

Jackson的例子(com.fasterxml.jackson.core:jackson-databind:2.0.6):

快速而肮脏的方法:

    String json = "{\n" +
            "\"10516\": {\"estimated\": {\"value\": 10.0,\"text\": \"10.0\"},\"completed\": {\"value\": 7.5,\"text\": \"7.5\"}},\n" +
            "\"10244\": {\"estimated\": {\"value\": 15.5,\"text\": \"15.5\"},\"completed\": {\"value\": 7.5,\"text\": \"7.5\"}},\n" +
            "\"10182\": {\"estimated\": {\"value\": 12.0,\"text\": \"12.0\"},\"completed\": {\"value\": 10.0,\"text\": \"10.0\"}}\n" +
            "}";

    ObjectMapper mapper = new ObjectMapper();
    TypeReference<Map> typeRef = new TypeReference<Map>(){};
    try {
        Map<String, Object> map = mapper.readValue(json, typeRef);
        System.out.println(map);
    } catch (IOException e) {
        // log error
    }

输出:

{10516={estimated={value=10.0, text=10.0}, completed={value=7.5, text=7.5}}, 10244={estimated={value=15.5, text=15.5}, completed={value=7.5, text=7.5}}, 10182={estimated={value=12.0, text=12.0}, completed={value=10.0, text=10.0}}}

这样您就可以解析任何 JSON字符串。您当然可以访问所有JSON字段,只要您转换为相应的类型:

        Map item = (Map) map.get("10182");
        System.out.println(item);

        Map estimated = (Map) item.get("estimated");
        Double value = (Double) estimated.get("value");
        System.out.println(value);

        String text = (String) estimated.get("text");
        double newValue = value + 10;
        System.out.println("old text: "+text+", new value: "+newValue);

输出:

{estimated={value=12.0, text=12.0}, completed={value=10.0, text=10.0}}
12.0
old text: 12.0, new value: 22.0

更清洁,更好的方法

当然,更好的方法是定义一些模型类,如下所示:

class Model {
    ModelContent estimated;
        public ModelContent getEstimated() {return estimated;}
        public void setEstimated(ModelContent estimated) { this.estimated = estimated;}
    ModelContent completed;
        public ModelContent getCompleted() {return completed;}
        public void setCompleted(ModelContent completed) { this.completed = completed;}
    public Model() {}
}


class ModelContent {
    Double value;
        public Double getValue() {return value;}
        public void setValue(Double value) { this.value = value;}
    String text;
        public String getText() {return text;}
        public void setText(String text) {this.text = text;}
    public ModelContent() {}
}

并将它们用作根映射的值类型:

    ObjectMapper mapper = new ObjectMapper();
    TypeReference<Map<String,Model>> typeRef = new TypeReference<Map<String,Model>>(){};
    try {
        Map<String, Model> map = mapper.readValue(json, typeRef);
        Model item = map.get("10182");
        ModelContent completed = item.getCompleted();
        Double completedValue = completed.getValue();
        System.out.println("value: "+completedValue);
    } catch (IOException e) {
        // log error
    }

输出:

value: 10.0

答案 1 :(得分:0)

String jsonString="YOUR JSON STRING";
JSONObject mainObject=new JSONObject(jsonString);
Iterator<String> keys= mainObject.keys();
while (keys.hasNext()) 
    {
    String keyValue = (String)keys.next();
    JSONObject obj1 = mainObject.getJSONObject(keyValue);
    JSONObject estimatedObj = obj1.getJSONObject("estimated");
    JSONObject completedObj = obj1.getJSONObject("completed");
    System.out.print(keyvalue+"-"+estimatedObj.getString("text")+"-"+completedObj.getString("text"));
    System.out.println();
    }