1

I want to create a string which will look like the following:

 "[{"product_id":"123","name":"stack"},{"product_id":"456","name":"overflow"}]"

I have the product_id and name in two arrays. How can I create the above type of string most efficiently?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
saurabh ranu
  • 1,351
  • 6
  • 18
  • 24
  • I saw your comment on your [related deserialization question](http://stackoverflow.com/q/7099803/193874). Do you want to do the conversion from two arrays or from a `List`? – Rob Tanzola Aug 18 '11 at 21:19

4 Answers4

14

This looks like JSON. You should use a JSON library.

There are plenty out there (see this page, scroll almost to the end), but

are the ones I'd use.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    Nice answer with samples and everything. – Oscar Gomez Aug 19 '11 at 01:27
  • +1 @Sean Patrick Floyd, good recommendations on using a library and which one to use - that will provide flexibility and code clarity if there is a future requirement to add new fields, have more complex hierarchical relationships, etc. – Rob Tanzola Aug 19 '11 at 02:00
0

You may want to have a look on the Java String Formatter: http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html

Anis Abboud
  • 1,328
  • 2
  • 16
  • 23
0

I'm assuming you have 2 arrays one containing and product_id and the other containing the values and both of these are in correct order.

I would create my own java object (POJO) that had two fields product_id and name then take your two arrays and create one array (or list) of POJOs each POJO containing the product_id and name. Then I would just use Jackson or Gson to create my JSON.

These libraries give you a JSON representation of a Java Objects, so in this case you will have to make sure that you have a list or even an array of objects which contain your product_id and name.

If you insist on doing it the hard-way (or no external library way), then I would create a template String and repeatedly call replace on it and add it to a StringBuffer. The StringBuffer is important if the string can be very large. Something like:

String template = "{\"product_id\":\"productId\",\"name\":\"productName\"}";
StringBuffer result = new StringBuffer("[");

for(int i=0; i<myProductArray.length; i++){
   String temp = template.replace("productId",myProductArray[i]);
   temp = temp.replace("productName",myNameArray[i]);
   if(result.length() > 1)
      result.append(",");
   result.append(temp);
}
result.append("]");
return result.toString();

If you can replace the template String with a StringBuffer and manipulate that directly, it'll make a ton of difference performance wise for large Strings.

Regards,

Ali
  • 12,354
  • 9
  • 54
  • 83
0

If it turns out you already have a List<ProductInformation>, then the default serialization with flexjson with an exclude will give you the string:

String jsonResult = new JSONSerializer().exclude("*.class").serialize(listOfProductInformation);

Regarding performance, your best bet is to use a good profiler to look at your overall application. This will identify your true hotspots/bottlenecks and if it turns out this serialization process is a hotspot, you can spend time tuning as @Ali mentions and running it back through the profiler or performance test harness to measure the impact.

Rob Tanzola
  • 1,785
  • 14
  • 11