I have pass two values from ajax to my servlet.
I used
JsonObject data = new Gson().fromJson(request.getReader(), JsonObject.class);
System.out.println(data);
and this is the output
{"0":"31/01/2017","1":"19/01/2017"}
Now I want to convert this data into a java arraylist but not really sure how.
I tried
Gson googleJson = new Gson();
JsonObject data = googleJson.fromJson(request.getReader(), JsonObject.class);
System.out.println(data);
JsonArray jsonArr = data.getAsJsonArray();
// jsonArr.
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
for(int i=0; i< jsonObjList.size(); i++) {
System.out.println(jsonObjList.get(i));
}
But got an error
java.lang.IllegalStateException: This is not a JSON Array.
Someone help me please? thanks.
Create instance of JsonArray then add json element to that array using key.
Here is your solution :
Gson googleJson = new Gson();
JsonObject data = googleJson.fromJson(request.getReader(), JsonObject.class);
System.out.println(data);
JsonArray jsonArr = new JsonArray();
for(Entry<String, JsonElement> entry : data.entrySet()) {
jsonArr.add(data.get(entry.getKey()));
}
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
for(int i = 0; i < jsonObjList.size(); i++) {
System.out.println(jsonObjList.get(i));
}
For a Json to be a valid JsonArray object you should have it to a proper format. Can you change your json string return from your ajax? If yes you should change it to something like this:
Gson googleJson = new Gson();
JsonObject data = googleJson.fromJson("{test: [{\"0\":\"31/01/2017\"},{\"1\":\"19/01/2017\"}]}", JsonObject.class);
System.out.println(data);
JsonArray jsonArr = data.getAsJsonArray("test");
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
for(int i=0; i< jsonObjList.size(); i++) {
System.out.println(jsonObjList.get(i));
}
If not you should parse it by your self and convert it to everything you want.
I'm trying to get the content of msg which is ERROR. I'm doing that by transforming string return into an array.
Now the solution 1, works perfectly fine using import org.json.JSONArray;:
Vector<ClsReturn> ret = null;
ret = ds.ind(coll,uri );
JSONArray array = new JSONArray(" [{\"type\":1, \"txt\":\"ERROR\"}]");
int i = 0;
JSONObject myJsonObject = new JSONObject();
while(i < array.length()){
myJsonObject = array.getJSONObject(i);
System.out.println(myJsonObject.getString("txt"));
i++;
}
The above solution allows me tro retrieve the content of "txt", which is "ERROR".
However (below code) when I try to use the reutrn of String jsReturn = JSONArray.toJSONString(ret); I'm not capable to do that because the library I'm using doesn't support JSONArray.toJSONString, so I need to import org.json.simple.JSONArray; which in turn doesn't allow me to use array.getJSONObject(i) but both libraries org.json.simple.JSONArray and
org.json.JSONArray don't work together. Any workaround to get the desired outcome?
Vector<ClsReturn> ret = null;
ret = ds.ind(coll,uri );
String jsReturn = JSONArray.toJSONString(ret);//output(using simple.JSONArray) --> [{"type":1, "msg":"ERROR"}]
JSONArray array = new JSONArray(jsReturn);
int i = 0;
JSONObject myJsonObject = new JSONObject();
while(i < array.length()){
myJsonObject = array.getJSONObject(i);
System.out.println(myJsonObject.getString("txt"));
i++;
}
org.json.JSONArray has a toString() method that will output a JSON string. It also allows for indentation by using toString(int indent_factor)
I am working on an android app in which i send an http response to a server and get a response in string which is in JSON format, now on the basis of this response i need to generate a stacked column chart using AChartEngine for different equipments. The response string looks like as follows
[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":0,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":null,"EquipmentStatusDescription":null,"EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":0,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":null,"EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":561.60722222222023,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":"#008000","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":1,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.InCycle","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":134.57583333333525,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":"#FFFF00","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":"","EquipmentStatusTypeId":2,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.UnknownDowntime","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0}],"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":0,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":null,"EquipmentStatusDescription":null,"EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":0,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":null,"EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":495.61333333332925,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":"#008000","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":1,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.InCycle","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":200.56972222222623,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":"#FFFF00","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":"","EquipmentStatusTypeId":2,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeNam
Now this is a response for five different equipments and how can i use this JSON string to an array so that i can extract the data of each equipment and send it to AChartEngine for drawing of chart?
You can convert the response string to JSONObject. The response you posted is JSONArray.
JSONArray jsonArray = new JSONArray(responseString);
int size = jsonArray.length();
ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
for(int i= 0; i< size; i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
// use this jsonObject to get the value by passing the keys.
String value = jsonObject.optString(key);
//here is your array list of jsonobjects
arrayList.add(jsonObject);
}
Use below Code for Pass JsonArray and store Value into ArrayList.
JSONArray jArray = new JSONArray(mRes);
ArrayList<String> array1 = new ArrayList<String>();
for(int i= 0; i< size; i++) {
JSONObject jObject = jArray.getJSONObject(i);
String mValue = jObject.getString("your_json_object_name");
array1.add(mValue);
}
Use Gson api, it allows you to direct mapping of json objects to java objects. check following links for further reference:
http://code.google.com/p/google-gson/
https://sites.google.com/site/gson/gson-user-guide