How to convert String to JsonObject

2019-02-12 01:53发布

I am using a httprequest to get Json from a web into a string.

It is probably quite simple, but I cannot seem to convert this string to a javax.json.JsonObject.

How can I do this?

2条回答
唯我独甜
2楼-- · 2019-02-12 02:41

Since the above reviewer didn't like my edits, here is something you can copy and paste into your own code:

private static JsonObject jsonFromString(String jsonObjectStr) {

    JsonReader jsonReader = Json.createReader(new StringReader(jsonObjectStr));
    JsonObject object = jsonReader.readObject();
    jsonReader.close();

    return object;
}
查看更多
The star\"
3楼-- · 2019-02-12 02:53
JsonReader jsonReader = Json.createReader(new StringReader("{}"));
JsonObject object = jsonReader.readObject();
jsonReader.close();

See docs and examples.

查看更多
登录 后发表回答