Extra quotes around JSON string values parsed using javax.json.stream.JsonParser and Java streams

问题: I'm parsing some JSON received from a REST service call using Java streams JsonParser.getObjectStream(), and filtering for particular values, but was never getting a match...

问题:

I'm parsing some JSON received from a REST service call using Java streams JsonParser.getObjectStream(), and filtering for particular values, but was never getting a match where I was expecting one. Digging deeper, I found that the string values being returned containing extra double-quotes at the beginning and end.

Here's some code to illustrate. First I show the "direct" method of getting values, then the streams method.

package com.company.jsonptest;

import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.stream.JsonParser;

public class GetStringTest {
    public static void main(String[] args) {
        System.out.println("Direct...");
        JsonParser parser = Json.createParser(new StringReader(json));
        parser.next();
        JsonObject jsonObject = parser.getObject().asJsonObject();
        String username = jsonObject.getString("firstname");
        String name = jsonObject.getString("lastname");
        System.out.println("firstname: " + username);
        System.out.println("lastname: " + name);

        System.out.println("Streams...");
        JsonParser parser2 = Json.createParser(new StringReader(json));
        parser2.next();
        parser2.getObjectStream().forEach(entry -> {
            String key = entry.getKey();
            String value = entry.getValue().toString();
            String type = entry.getValue().getValueType().toString();
            System.out.println(key + ": " + value + " (" + type + ")");
        });
    }

    private static String json = "{n"
        + "  "firstname": "John",n"
        + "  "lastname": "Smith"n"
        + "}";
}

Here's the output. Notice the extra quotes. I've included the JsonValue type as an extra check.

Direct...
firstname: John
lastname: Smith

Streams...
firstname: "John" (STRING)
lastname: "Smith" (STRING)

Is there a way to get around this, apart from adding an extra step to strip off the quotes?


回答1:

The problem is that getObjectStream() just iterates the elements as JsonValues, which loose the typing methods that JsonObject has in your first approach. The big difference is that you use toString() (which just returns the json "thing" behind the :) instead of getString() (which interprets that "thing" as a String).

In order to get this info (and the methods like getString()) back, you have to cast the JsonValue to a JsonString

String value = ((JsonString) entry.getValue()).getString();

This will return the values without quotes.


See this Javadoc for an example where they cast an element they search for to JsonArray:

https://static.javadoc.io/javax.json/javax.json-api/1.1.0-M1/javax/json/stream/JsonParser.html

  • 发表于 2019-07-04 18:38
  • 阅读 ( 262 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除