Rolando
Rolando

Reputation: 62596

How to properly format JSON string in java?

I have a jersey client that is getting JSON from a source that I need to get into properly formatted JSON:

My JSON String looks like the folllowing when grabbing it via http request:

{
    "properties": [
        {
            someproperty: "aproperty",
            set of data: {
                keyA: "SomeValueA",
                keyB: "SomeValueB",
                keyC: "SomeValueC"
            }
        }
    ]
}

I am having problems because the json has to be properly formatted and keyA, keB, and keyC are not surrounded in quotes. Is there some library that helps add quotes or some best way to go about turning this string to properly formatted json? Or if there is some easy way to convert this to a json object without writing a bunch of classes with variables and lists that match the incoming structure?

Upvotes: 6

Views: 46655

Answers (4)

bmargulies
bmargulies

Reputation: 99993

Your string isn't JSON. It's something that bears a resemblance to JSON. There is no form of JSON that makes those quotes optional. AFAIK, there is no library that will reads your string and cope with the missing quotes and then spit it back out correctly. You need to find the code that produced this and repair it to produce actual JSON.

Upvotes: 3

Martín Schonaker
Martín Schonaker

Reputation: 7305

I like Flexjson, and using lots of initilizers:

  public static void main(String[] args) {

    Map<String, Object> object = new HashMap<String, Object>() {
        {
            put("properties", new Object[] { new HashMap<String, Object>() {
                {
                    put("someproperty", "aproperty");
                    put("set of dada", new HashMap<String, Object>() {
                        {
                            put("keyA", "SomeValueA");
                            put("keyB", "SomeValueB");
                            put("keyC", "SomeValueC");
                        }
                    });
                }
            } });
        }
    };

    JSONSerializer json = new JSONSerializer();
    json.prettyPrint(true);
    System.out.println(json.deepSerialize(object));
  }

results in:

{
  "properties": [
    {
        "someproperty": "aproperty",
        "set of dada": {
            "keyA": "SomeValueA",
            "keyB": "SomeValueB",
            "keyC": "SomeValueC"
        }
    }
  ]
}

Upvotes: 3

Cruis
Cruis

Reputation: 377

you can use json-lib. it's very convenient! you can construct your json string like this:

JSONObject dataSet = new JSONObject();
dataSet.put("keyA", "SomeValueA") ;
dataSet.put("keyB", "SomeValueB") ;
dataSet.put("keyC", "SomeValueC") ;

JSONObject someProperty = new JSONObject();
dataSet.put("someproperty", "aproperty") ;

JSONArray properties = new JSONArray();
properties.add(dataSet);
properties.add(someProperty);

and of course you can get your JSON String simply by calling properties.toString()

Upvotes: 5

JR Galia
JR Galia

Reputation: 17269

You can use argo, a simple JSON parser and generator in Java

Upvotes: 0

Related Questions