简体   繁体   中英

How to convert nested Json in string into Json object in java

I am looking for some Java library, which can convert below give String into Json object.

Input: String reading from file.

{ "product": "{\"sku\":\"rtwre-rtwe\",\"price\":\"50.90\",\"currency_code\":\"SGD\",\"quantity\":1}", "is_organic": "0", "can_claim": "0", "t": "r", "device": "Phone", "amount_transactions": "0" }

Expected output: In some generic Java Json object.

{
   "product": {
      "sku": "rtwre-rtwe",
      "price": "50.90",
      "currency_code": "SGD",
      "quantity": 1
   },
   "is_organic": "0",
   "can_claim": "0",
   "t": "r",
   "device": "Phone",
   "amount_transactions": "0"
}

Imp points: This is sample code, I have more dynamic json and don't have any Java object corresponding to my json. I can have string json in any key. It's not specific to particular key. I am looking for more generic code.

Here my goal if I read value of key "product" it should return Json instead of String. I want to read $.product.price using JsonPath library. http://jsonpath.com/

Edit1: I don't have much experience with Gson, Jackson and JsonObject libraries, but I tried whatever I could do. If you had handled the same scenario, please help me out.

To resolve it you can use :

JSONObject jsonObj = new JSONObject(myStringValue);
String myJsonStructureAsString = jsonObj.toString();

Where JSONObject is org.json.JSONObject form lib json-org.v2017.05.16.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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