How you iterate and add
for(int i=0;i<jsonArray.length;i++)
{
JSONObject json_data = jArray.getJSONObject(i);
imgnfo.id = json_data.getInt("id_key");
imgnfo.name = json_data.getString("name_key");
imgnfo.thumb = json_data.getString("thumb_key");
imgnfo.info = json_data.getString("info_key");
myArray.add(new image_data());
}
Just add proper keys names. I didn't know them.
Answer from Nikola Despotoski on Stack OverflowHow you iterate and add
for(int i=0;i<jsonArray.length;i++)
{
JSONObject json_data = jArray.getJSONObject(i);
imgnfo.id = json_data.getInt("id_key");
imgnfo.name = json_data.getString("name_key");
imgnfo.thumb = json_data.getString("thumb_key");
imgnfo.info = json_data.getString("info_key");
myArray.add(new image_data());
}
Just add proper keys names. I didn't know them.
Yo should post what the json data looks like. Assuming the json data looks something like
...,{
"image_data": {
"name": "image name","thumb":"thumbpath","path":"path"}
},{
"image_data": {
"name": "image name 2","thumb":"thumbpath 2","path":"path 2"}
}
Then you would extract the values like so:
ArrayList<image_data> imageArray = new ArrayList<image_data>();
for (int i=0; i<jsonArray.length(); i++) {
image_data img = new image_data();
Object jo = jsonArray.get(i);
image_data.name = jo.name;
image_data.thumb = jo.thumb;
image_data.path = jo.path;
imageArray.add(new image_data());
}
Consider using getters for you image_data class. Also consider renaming it to ImageData
java - Put ArrayList into param JsonObject - Stack Overflow
java - Creating an ArrayList from a JSON Object - Stack Overflow
java - How to convert ArrayList to JSON object - Stack Overflow
Json object to java arraylist - Stack Overflow
public class Semester {
private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_CREDITS = "credits";
private static final String JSON_GPA = "gpa";
private static final String JSON_CUMULATIVECREDITS = "cumulativecredits";
private static final String JSON_CUMULATIVEGPA = "cumulativegpa";
private static final String JSON_COURSES = "courses";
private UUID id;
private String title;
private int credits;
private double GPA;
private int cumulativeCredits;
private double cumulativeGPA;private ArrayList<Course> courses;
public Semester() {
id = UUID.randomUUID();
courses = new ArrayList<Course>();
for (int i = 0; i < 6; i++) {
courses.add(new Course());
}
}
public Semester(JSONObject json) throws JSONException {
id = UUID.fromString(json.getString(JSON_ID));
title = json.getString(JSON_TITLE);
credits = json.getInt(JSON_CREDITS);
GPA = json.getDouble(JSON_GPA);
cumulativeCredits = json.getInt(JSON_CUMULATIVECREDITS);
cumulativeGPA = json.getDouble(JSON_CUMULATIVEGPA);//what do i put here?
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put(JSON_ID, id.toString());
json.put(JSON_TITLE, title);
json.put(JSON_CREDITS, credits);
json.put(JSON_GPA, GPA);
json.put(JSON_CUMULATIVECREDITS, cumulativeCredits);
json.put(JSON_CUMULATIVEGPA, cumulativeGPA);JSONArray array = new JSONArray(courses);
json.put(JSON_COURSES, array);
return json; } .........
This is followed by a bunch of getters and setters for each private variable.
The first bolded item is the private ArrayList of a custom class that I made.
The second bolded item is what I need help with: how to retrieve the custom ArrayList just like the other variables.
Lastly, the third bolded item is my current idea of how to put an ArrayList into a JSONObject, but I don't know how to retrieve it. If there is another way, please tell me. Thank you.
you need to make a JSON Array first and then store that
private String mUrl;
private ArrayList<String> mUrlDove;
HashMap<String, String> params = new HashMap<String, String>();
params.put("url", mUrl);
JSONArray jsArray = new JSONArray(mUrlDove);
params.put("urlDove", jsArray.toString());
mUrl = app.getInstance().getmUrlRestWS() + getString(R.string.path);
JsonObjectRequest mRequest = new JsonObjectRequest(
mUrl, new JSONObject(params),
createMyReqSuccessListener(),
createMyReqErrorListener()) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return app.getInstance().createBasicAuthHeader();
}
};
try passing JSONObject instead of hashmap
JSONObject params = new JSONObject();
params.put("url", "www.secret.com");
JSONArray urlDove = new JSONArray();
urlDove.put("www.google.com");
urlDove.put("www.yahoo.com");
params.put("urlDove", urlDove);
JsonObjectRequest mRequest = new JsonObjectRequest(
mUrl, params,
createMyReqSuccessListener(),
createMyReqErrorListener()) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return app.getInstance().createBasicAuthHeader();
}
};
for reference in parsing json https://stackoverflow.com/a/17810270/4810752
You can use the GSON library to accomplish this.
ArrayList<String> mylist = new ArrayList<String> ();
mylist.add("abc");
mylist.add("cfd");
mylist.add("ert");
mylist.add("fg");
mylist.add("ujk");
String json = new Gson().toJson(mylist);
You can refer to the GSON User Guide for more support.
JSONArray jsArray = new JSONArray(mylist);
Or
Gson gson = new Gson();
String jsonArray = gson.toJson(mylist);
Get java-json.jar and Gson.jar here
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.
If you want to work with json go this library, this library has a nice support https://code.google.com/p/google-gson/.
This is an example:
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
Thanks
org.json and org.json.simple JSON parser use raw types of collections underneath. If you're looking for good Generics support try Google Gson. Here's how you would go about serializing your generic ArrayList with Gson:
Gson gson = new Gson();
ArrayList<String> arrayList= (ArrayList<String>) ClassName.getArrayList();
// Serializing to a JSON element node
JsonElement jsonElement = gson.toJsonTree(arrayList);
System.out.println(jsonElement.isJsonArray()); // true
// Or, directly to JSON string
String json = gson.toJson(arrayList);
System.out.println(json);
Here's how you would deserialize the same JSON string with its Generics intact:
Type type = new TypeToken<ArrayList<String>>(){}.getType();
ArrayList<String> arrayList = gson.fromJson(json, type);
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.getString(i));
}
}
I've done it using Gson (by Google).
Add the following line to your module's build.gradle:
dependencies {
// ...
// Note that `compile` will be deprecated. Use `implementation` instead.
// See https://stackoverflow.com/a/44409111 for more info
implementation 'com.google.code.gson:gson:2.8.2'
}
JSON string:
private String jsonString = "[\n" +
" {\n" +
" \"id\": \"c200\",\n" +
" \"name\": \"Ravi Tamada\",\n" +
" \"email\": \"ravi@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c201\",\n" +
" \"name\": \"Johnny Depp\",\n" +
" \"email\": \"johnny_depp@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c202\",\n" +
" \"name\": \"Leonardo Dicaprio\",\n" +
" \"email\": \"leonardo_dicaprio@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c203\",\n" +
" \"name\": \"John Wayne\",\n" +
" \"email\": \"john_wayne@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c204\",\n" +
" \"name\": \"Angelina Jolie\",\n" +
" \"email\": \"angelina_jolie@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c205\",\n" +
" \"name\": \"Dido\",\n" +
" \"email\": \"dido@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c206\",\n" +
" \"name\": \"Adele\",\n" +
" \"email\": \"adele@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c207\",\n" +
" \"name\": \"Hugh Jackman\",\n" +
" \"email\": \"hugh_jackman@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c208\",\n" +
" \"name\": \"Will Smith\",\n" +
" \"email\": \"will_smith@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c209\",\n" +
" \"name\": \"Clint Eastwood\",\n" +
" \"email\": \"clint_eastwood@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\", You coud use Gson library, that handles lists properly, instead.
Usage example:
class BagOfPrimitives {
private int value1;
private String value2;
private transient int value3;
public BagOfPrimitives(int value1, String value2, int value3) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
BagOfPrimitives obj1 = new BagOfPrimitives(1, "abc", 3);
BagOfPrimitives obj2 = new BagOfPrimitives(32, "gawk", 500);
List<BagOfPrimitives> list = Arrays.asList(obj1, obj2);
Gson gson = new Gson();
String json = gson.toJson(list);
// Now json is [{"value1":1,"value2":"abc"},{"value1":32,"value2":"gawk"}]
You could override the toString method in your Site class to return new JSONObject(this).toString