0

I have an arraylist like:

List skus = new ArrayList();  

and the output is

[{value=4, sequence=1, availableinventory=50, defining=size, skuid=24846457, partnumber=602016000967318}, 
 {value=6, sequence=2, availableinventory=50, defining=size, skuid=24846459, partnumber=602016000967319}, 
 {value=8, sequence=3, availableinventory=50, defining=size, skuid=24846462, partnumber=602016000967320}, 
 {value=10, sequence=4, availableinventory=50, defining=size, skuid=24846468, partnumber=602016000967321}, 
 {value=12, sequence=5, availableinventory=50, defining=size, skuid=24846476, partnumber=602016000967322}, 
 {value=14, sequence=6, availableinventory=50, defining=size, skuid=24846484, partnumber=602016000967323}, 
 {value=16, sequence=7, availableinventory=50, defining=size, skuid=24846492, partnumber=602016000967324}]

But i want to convert the above arraylist to JSON response which is a string builder like below .

StringBuilder jsonUIRespesponse = new StringBuilder();

[{"skuid":"24845337","value":"16","partnumber":"602016000967240","availableinventory":"50","sequence":"7","defining":"size"},
{"skuid":"24845325","value":"6","partnumber":"602016000967235","availableinventory":"50","sequence":"2","defining":"size"},
{"skuid":"24845326","value":"8","partnumber":"602016000967236","availableinventory":"50","sequence":"3","defining":"size"},
{"skuid":"24845327","value":"10","partnumber":"602016000967237","availableinventory":"50","sequence":"4","defining":"size"},
{"skuid":"24845328","value":"12","partnumber":"602016000967238","availableinventory":"50","sequence":"5","defining":"size"},
{"skuid":"24845329","value":"14","partnumber":"602016000967239","availableinventory":"50","sequence":"6","defining":"size"},
{"skuid":"24845324","value":"4","partnumber":"602016000967234","availableinventory":"50","sequence":"1","defining":"size"}]

2 Answers 2

1

You can do this very easily with a mapper that converts from Java Objects into JSON constructs. I use Jackson but there are other implementations.

First be sure you have Jackson library in your Java project. If you're using Maven you have to add a dependency. Just to be clear, AFAIK does not exist in JDK a common Json serializer/deserializer, so you must add a library in your project, i.e. a reference implementation of JSR 353: Java API for JSON Processing

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.3</version>
</dependency>

Then you can convert Java Objects in Json with ObjectMapper utility class.

import com.fasterxml.jackson.databind.ObjectMapper;
...
ObjectMapper mapper = new ObjectMapper();
String jsonResponse = mapper.writeValueAsString(skus);

or append to a StringBuilder

StringBuilder sb = new StringBuilder();
sb.append(mapper.writeValueAsString(skus));
Sign up to request clarification or add additional context in comments.

9 Comments

I would want to append the sorted JSON values to jsonUIRespesponse (StringBuilder jsonUIRespesponse = new StringBuilder();) , So is there any other way without using Jackson. If so plz help
It is not clear how to sort json values. Your output example is not sorted. In any case, if you want a sorted json, I suggest to sort the java objects, I mean the values before the conversion.
Actually the sorted values are present in Arraylist "skus". So i just want to append the same order/values present in "skus" to stringbuilder "jsonUIRespesponse " , but i would want it as JSON format in stringbuilder "jsonUIRespesponse " (with double quotes and colon).
the writeValueAsString method just converts the Java objects in json, returning a string with double quotes and colons. Have you tried it?
i Could not use that because the import function is throwing error ."com.fasterxml cannot be resolved ". So could you pls tell me how do i use ObjectMapper without the import statement?
|
0

You can use the Jackson Library.

  ObjectMapper OBJECT_MAPPER    = new ObjectMapper();

StringWriter stringWriter = new StringWriter();
     OBJECT_MAPPER.writeValue(stringWriter, YOUR_LIST);
    return stringWriter.toString();

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.