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.
Answer from rageandqq on Stack OverflowYou 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
android - convert ArrayList to JSONArray - Stack Overflow
Powershell arraylist to json
Convert normal Java Array or ArrayList to Json Array in android - Stack Overflow
How do I save an ArrayList<(Custom object here)> with JSON?
Videos
If I read the JSONArray constructors correctly, you can build them from any Collection (arrayList is a subclass of Collection) like so:
ArrayList<String> list = new ArrayList<String>();
list.add("foo");
list.add("baar");
JSONArray jsArray = new JSONArray(list);
References:
- jsonarray constructor: http://developer.android.com/reference/org/json/JSONArray.html#JSONArray%28java.util.Collection%29
- collection: http://developer.android.com/reference/java/util/Collection.html
Use Gson library to convert ArrayList to JsonArray.
Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(myCustomList).getAsJsonArray();
Your description doesn't match your post title. What is your actual question?
Both Powershell and JSON don't particularly care about the structure of your data. In PS you can have an array of various objects (it sees everything as object anyway) and JSON allows for optional fields so the missing fields in the first value won't impact the structure.
# Using a hash object here for convenience
$item1 = @{ basePrice = 100; category = "some category"; packageWeightUnit = "g"; productType = "Some type" }
$item2 = @{ basePrice = 100; category = "some category"; packageWeightUnit = "g"; productType = "Some type"; attributes = @{ colour = @("blue", "red"); hight = @(100) } }
# Assuming you have more items than this...
$list = New-Object System.Collections.ArrayList
$list += $item1
$list += $item2
ConvertTo-Json $list -depth 4
To go the other way, given the previous JSON
$readItems = ConvertFrom-Json $json
$list.add(@{"name"=$red.name;
"description"=$desc;
"coverImage"=$red.coverImage;
"ean"=$red.ean;
"packageWeightValue"=$red.packageWeightValue;
"packageWeightUnit"=$red.packageWeightUnit;
"basePrice"=$red.basePrice;
"vat"=$red.vat;
"stockLevel"=$red.stockLevel;
"sku"=$red.sku;
"gallery"=$galerija;
"brand"=$red.brand;
"productType"=$red.productType;
"category"=$red.category;
attributes = @{ "key features" = @($features.naziv)}
})
and i am getting no error but output looks like
"attributes": {
"key features": "PARIS Hidratacija Balzam"
},
but it should be
"attributes": {
"key features": ["PARIS", "Hidratacija", "Balzam""
},
If you want or need to work with a Java array then you can always use the java.util.Arrays utility classes' static asList() method to convert your array to a List.
Something along those lines should work.
String mStringArray[] = { "String1", "String2" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
Beware that code is written offhand so consider it pseudo-code.
ArrayList<String> list = new ArrayList<String>();
list.add("blah");
list.add("bleh");
JSONArray jsArray = new JSONArray(list);
This is only an example using a string arraylist
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.
To convert ArrayList to Json, just download Open Source json utility from: http://json.org/java/ or Jar file from here
And just do:
JSONArray jsonAraay = new JSONArray(your_array_list);
That's it
Note: You should have setter/getter in your POJO/MODEL class to convert arraylist to json
Don't bother with org.json, use Jackson all the way:
// list is a List<MyData>
final ObjectMapper mapper = new ObjectMapper();
final Map<String, MyData> map = new HashMap<>();
for (final MyData data: list)
map.put(data.fname, data);
final JsonNode json = mapper.valueToTree(map);
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