0

I have always used XML for returning data from my servlets and i have decided to have a go with JSON.Simple.

I currently have;

    public JSONObject loadDetails() throws IOException {


    JSONArray list = new JSONArray();
    list.add(new Car("car1",3,4,3,2,4,5));
    list.add(new Car("car2",3,4,3,2,4,5));

    StringWriter out = new StringWriter();

    list.writeJSONString(out);

     System.out.println(out.toString());

    return null;
}

I am trying to store some car details, however when i try and print i just get the object names rather than the correct JSON value. My car class implements JSONStreamAware eg;

 public void writeJSONString (Writer out) throws IOException{
    LinkedHashMap obj = new LinkedHashMap();
    obj.put("carname", carname);
    obj.put("engine", engine);

    JSONValue.writeJSONString(obj, out);
}

Have i missed something here?

1

1 Answer 1

1

I am sorry, I know that this is not what you are explicitly asking for but I will definitely recommend using Gson for json parsing. You jsut specify several annotations and everything is magically serialized / deserialized. Read about gson here. Again sorry that this is not exact answer of the question, but still I hope it will help you get started in json.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.