0

I have a dilemma. Given a string with the following format:

{
"key1": "value1",
"key2": "value2",
"key3": {"a": "b", "c": "d"}
}

I would like to create a JSONObject in Java that treats the value of key3 as a string, rather than treating it as a nested JSONObject because transforming the value of key3 into a nested JSONObject distorts the order of the key value pairs which is important for me. I have tried googling and stackoverflowing around and have not found anyone running into a similar problem. Any ideas or suggestions? I am using org.json.

The ultimate goal should be to tell JSON treat all the values as strings and do not create any nested objects.

user1848861
  • 37
  • 1
  • 5
  • What have you used? Show some example. – xAqweRx Jun 29 '16 at 06:37
  • 1
    If you're relying on the order of properties in JSON objects, then you're doing something wrong. Use an array if order matters: [{"a": "b"}, {"c": "d"}] for example. – JB Nizet Jun 29 '16 at 06:42
  • @JBNizet Unfortunately, the data format has already been determined, and I can't change it. Is there a way around it? I would like to have no nested json objects. All values should be treated as a string. – user1848861 Jun 29 '16 at 06:48
  • did you try to use map for key3? – Minh Jun 29 '16 at 07:03
  • What is the desired representation you need for key3? .. Is it like "a:b,c:d" – Tuhin Jun 29 '16 at 07:04
  • Hi @EOF, my desire representation would be {"a": "b", "c": "d"}. This entire thing should be treated as a string – user1848861 Jun 29 '16 at 07:17
  • Hi @Minh thanks for following up :) what do you mean by using a map for key3? My goal is to find a more generalized solution to treat all my values as strings i.e. Json should not create any nested Json objects. (Only create a 1 level json object) – user1848861 Jun 29 '16 at 07:18
  • use a java-json library like jackson or gson – Sampada Jun 29 '16 at 07:26
  • If you can change json design nor library -> so you can keep the json design and modify the library. You can refer to @gary answer in [this thread](http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv). – Minh Jun 29 '16 at 07:46

0 Answers0