org.json.JSONOrderedObject Java library

Hi,

First of all, this is my first JSON in Java experience.

I was getting 400 - bad request for extraction call (/Extractions/Extract). Then I've tried to sent just a json string with property order exactly as it was in the example and it was working. A bit tricky.

I don't think my library (com.googlecode.json-simple) can maintain the property order within a JSONObject. In the API's Java examples there's a org.json.JSONOrderedObject - I was not able to find out which JSON library contains this class.

Thanks for any advice.

Filip

Best Answer

  • filip.balas
    filip.balas Explorer
    Answer ✓

    Ok, now I see these classes are included within the examples.

    Anyway I was hoping for more "Maven" way - I don't feel comfortable neither including this 3rd party to my source code nor creating and deploying artifact from those.

    I've resolved the issue with this simple work-around, still using json-simple, but substituting standard HashMap with LinkedHashMap.

    public class JSONOrderedObject extends LinkedHashMap<String, Object> implements Map<String, Object>, JSONAware, JSONStreamAware {

    @Overridepublic String toJSONString() {
    return JSONObject.toJSONString(this);}

    @Overridepublic void writeJSONString(Writer writer) throws IOException {
    JSONObject.writeJSONString(this, writer);}
    }

Answers