0

I have an array of object as ArrayList which I want to convert to JSON and send it to client over HttpResponse using Java and Jackson.

How I can do this?

3
  • 1
    using spring framework? Commented Mar 23, 2015 at 9:49
  • 1
    what you tried? Check mkyong.com/java/how-to-convert-java-object-to-from-json-jackson after use spring.... Commented Mar 23, 2015 at 9:50
  • Here I Have to write the objects first to a file. No conflict will happened if multiple users trying to access same code? Commented Mar 23, 2015 at 9:57

1 Answer 1

0

Assuming you already put jackson library and using Spring framework. Mentioned following line in your spring context file.

<!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

and Return the List/Map/Object from controller and it will internally convert it to JSON

For Non Spring:

doPost(HttpServletRequest req, HttpServletResponse resp)
{
  ObjectMapper mapper = new ObjectMapper();
  ArrayNode rootNode = mapper.readValue(req.getReader(), ArrayNode.class);
}

Source:

Passing array from javascript to java servlet using Jackson

http://software.danielwatrous.com/restful-java-servlet-serializing-tofrom-json-with-jackson/

Hope this helps :)

Do share your environment details if it was still not helpful and i will try to help :)

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

2 Comments

I don't have Spring framewark
i just updated my answer for non spring...servlet based scenario ..hope it helps

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.