Convert a gson.JsonObject to JSONObject
Asked Answered
O

3

27

I have a gson.JsonObject object. What is the easiest way to create a org.json.JSONObject object from it?

Orient answered 15/5, 2017 at 9:54 Comment(1)
TBH having two different JSON Parsers in a single project sounds like an hassle and you should check if you can't provide all components of your project with a single parser library, rather than switching back and forth between the two APIs.Toneytong
S
45

get JSON string again from JsonObject and parse it in JSONObject

JsonObject gson = new JsonParser().parse("{\"id\":\"value\"}").getAsJsonObject();

JSONObject jo2 = new JSONObject(gson.toString());
Sextet answered 15/5, 2017 at 9:57 Comment(0)
U
7
new org.json.JSONObject(gson.toJson(gson.JsonObject));
Uncurl answered 15/5, 2017 at 9:59 Comment(0)
N
2

Consider json below as a JSONObject.

JsonObject gson = JsonParser.parseString( json.toString() ),getAsJsonObject();

Newman answered 13/3, 2023 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.