There is a String in which Json is the answer, how can you parse this string into an object in Java? Are there any external or standard libraries?
- You already have the JSON string. Maybe you need to parse it into an object? - Sergey Gornostaev
- Yes, exactly, it means I asked the wrong question - Clarence
- There are, for example, Jackson and Gson . - Sergey Gornostaev
- Well, a lot of similar questions have already been asked. - eugeneek
|
1 answer
As already noted, you can use the library , but you can do everything manually. Example:
String json = "{paramsArray: [\"first\", 100]," + "paramsObj: {one: \"two\", three: \"four\"}," + "paramsStr: \"some string\"}"; JSONParser parser = new JSONParser(); Object obj = parser.parse(json); JSONObject jsonObj = (JSONObject) obj; System.out.println(jsonObj.get("paramsStr")); // some string JsonObject jo = jsonObj.get("paramsObj"); System.out.println(jo.get("three")); // four JsonArray ja = jsonObj.get("paramsArray"); System.out.println(ja.get(1)); // 100 |