Reputation: 1
I have two arrays,
Array 1 = [ a, b, c, d]
Array 2 = [ 1, 2, 3, 4]
I need to merge them like a dictionary/map (Array 1 is Keys and Array 2 is values) and convert that to JSON.
Below is the converted dictionary/map to JSON and final wanted result.
{"a":"1", "b":"2", "c":"3", "d":"4"}
Upvotes: 0
Views: 2030
Reputation: 1
org.json has a simple way to do it.
JSONArray keys = new JSONArray(Arrays.asList(array1));
JSONArray values = new JSONArray(Arrays.asList(array2));
JSONObject result = values.toJSONObject(keys);
Upvotes: 0
Reputation: 1
Thanks for all input special thanks to Pshemo. My working code:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class ArrayToJason {
public static void main (String args[]) throws Exception{
String [] value = {"a", "b" , "c", "d"};
String [] key = {"aa", "bb", "cc", "dd"};
Map <String, String> map1 = new HashMap();
Iterator <String> keys = Arrays.asList(value).iterator();
Iterator <String> values = Arrays.asList(key).iterator();
while (keys.hasNext() && values.hasNext()) {
map1.put(keys.next(), values.next());
}
ObjectMapper mapper = new ObjectMapper();
try {
String result = mapper.writeValueAsString(map1);
System.out.println("Jason format " + result);
}catch (JsonGenerationException e) {
e.printStackTrace();
}
}
}
**Jason format {"d":"dd","b":"bb","c":"cc","a":"aa"}
Upvotes: 0
Reputation: 9309
You can use jackson , and just merge given lists into Map
Map<String, String> map = new HashMap<>();
Iterator<String> keys = Arrays.asList("a","b").iterator();
Iterator<String> values = Arrays.asList("aa", "bb").iterator();
while (keys.hasNext() && values.hasNext()) {
map.put(keys.next(), values.next());
}
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(map);
Upvotes: 0
Reputation: 124215
It looks like you don't want to actually merge two arrays into one, but you want to create JSON object where its keys are stored in first array, and value in second array. In that case you can use org.json.JSONObject.
Here is simple example:
String[] keys = {"a", "b", "c", "d"};
String[] values = {"aa", "bb", "cc", "dd"};
JSONObject jsonObject = new JSONObject();
for (int i = 0; i<keys.length; i++){
jsonObject.put(keys[i], values[i]);
}
System.out.println(jsonObject);
Output {"a":"aa","b":"bb","c":"cc","d":"dd"}
Upvotes: 1
Reputation: 3791
As an example of a simple code.
If you have two arrays, lets say Array A and B, just iterate them and create a JSONObject of them, where array A is the keys and array B is the values.
Basic code example:
JSONObject map = new JSONObject()
for(int i =0; i<array.size(); i++) {
map.put(arrayA.[i],arrayB[i]);
}
return map;
Upvotes: 2
Reputation: 568
I assume you want the following:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < array1.length; i++)
{
builder.append("\"");
builder.append(array1[i]);
builder.append("\":\"");
builder.append(array2[i]);
builder.append("\"");
if (i < (array1.length - 1))
builder.append(", ");
}
String result = builder.toString();
Upvotes: -1
Reputation: 1462
The simplest way to convert array into JSON is:
JSONArray jsonArray = new JSONArray(Arrays.asList(myArray));
Upvotes: -1