How do I provide the following json output using java
I am trying to generate the following json output using the java net.sf.json libs but have been unsuccessful.
[ { "data": [ [ 1, 1, "Text" ], [ 2,开发者_JAVA百科 2, "Text" ], [ 3, 0, "Text" ], [ 5, 2, "Text" ] ], "label": "First Series" } ]
I have read on these forums Gson is my best bet going forward. Can anyone provide an example of how to generate this json using Gson or another suitable java based library.
Thanks in advance
i like this http://www.json.org/javadoc/org/json/JSONObject.html from http://json.org/java/
and JSONArray.
with those 2 objects:
JSONArray inner = new JSONArray()
inner.add(1);inner.add("text");
JSONObject outer = new JSONObject();
outer.put("data",inner);
outer.put("label", "stuff");
String out = outer.toString()
Gson
Gson is a Java library that can be used to convert Java Objects into its JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
import com.google.gson.Gson;
class Person {
private int age = 10;
private String name = "jigar";
}
Person obj = new Person();
Gson gson = new Gson();
String json = gson.toJson(obj);
http://json.org/java/
import org.json.JSONObject;
...
...
JSONObject json = new JSONObject();
json.put("city", "Mumbai");
json.put("country", "India");
...
String output = json.toString();
This is easy enough using a Java object like this:
public class GsonTest {
private List<DataItem> data;
private String label;
public GsonTest() {} // for Gson
public GsonTest(List<DataItem> data, String label) {
this.data = data;
this.label = label;
}
// ...
}
public class DataItem {
private int val1;
private int val2;
private String text;
public DataItem() {} // for Gson
public DataItem(int val1, int val2, String text) {
this.val1 = val1;
this.val2 = val2;
this.text = text;
}
// ...
}
Since your JSON format uses an array rather than an object for each data item (an object would make more sense based on your sample) you need to add a custom handler for serializing and deserializing DataItem
s to and from JSON arrays.
public class DataItemConverter implements JsonDeserializer<DataItem>,
JsonSerializer<DataItem> {
public DataItem deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
int val1 = array.get(0).getAsInt();
int val2 = array.get(1).getAsInt();
String text = array.get(2).getAsString();
return new DataItem(val1, val2, text);
}
public JsonElement serialize(DataItem src, Type typeOfSrc,
JsonSerializationContext context) {
JsonArray array = new JsonArray();
array.add(new JsonPrimitive(src.val1));
array.add(new JsonPrimitive(src.val2));
array.add(new JsonPrimitive(src.text));
return array;
}
}
Then you just need to register this converter when you create your Gson
instance and you're good to go! Since our DataItem
converter handles deserialization as well, you'll be able to deserialize the generated JSON as a List<GsonTest>
as well.
public static void testSerialization() {
List<DataItem> data = new ArrayList<DataItem>();
data.add(new DataItem(1, 1, "Text"));
data.add(new DataItem(2, 2, "Text"));
data.add(new DataItem(3, 0, "Text"));
data.add(new DataItem(5, 2, "Text"));
GsonTest test = new GsonTest(data, "First Series");
List<GsonTest> list = new ArrayList<GsonTest>();
list.add(test);
Gson gson = new GsonBuilder()
.registerTypeAdapter(DataItem.class, new DataItemConverter())
.create();
System.out.println(gson.toJson(list));
}
精彩评论