简体   繁体   English

如何使用 java 将 json 主体提取为字符串

[英]how to extract json body as string using java

I have a JSON response and i am extracting the body using getbody() command and storing the responses in list.我有一个 JSON 响应,我正在使用 getbody() 命令提取正文并将响应存储在列表中。 because i am passing multiple JSON at a time so each response i am storing in list as string value.因为我一次传递多个 JSON 所以每个响应我都作为字符串值存储在列表中。 How can i extract that JSON using JAVA?如何使用 JAVA 提取 JSON?

   response = request.post(route.payment());
            body = response.getBody();
            listofBody.add(body.asString());

above code is where i get the response and store that into list.上面的代码是我得到响应并将其存储到列表中的地方。 before converting to JSON i can do response.jsonPath().getList("company");在转换为 JSON 之前,我可以做response.jsonPath().getList("company"); to get the values获取值

To extract the element from JSON string you can use Google's Gson Library要从 JSON 字符串中提取元素,您可以使用 Google 的Gson

Example:例子:

JSONObject object = (JSONObject) JSONValue.parse(jsonString);
Set<String> keySet = object.keySet();
for (String key : keySet) {
    Object value = object.get(key);
    System.out.printf("%s=%s (%s)\n", key, value, value.getClass().getSimpleName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM