Reputation: 15
I'm having problems creating a Json output in an array without the array name. Currently when I create one I get the following Json response.
{
"values": [
{
"item1": "",
"item2": "",
"item3": "",
"item4": ""
}
]
}
But I want to remove following:
{
"values": [
]
}
And have the end result look like the following:
[
{
"item1": "",
"item2": "",
"item3": "",
"item4": ""
},
{
"item1": "",
"item2": "",
"item3": "",
"item4": ""
}
]
Here is my code I'm currently using.
JSONArray jsonArray = new JSONArray();
jsonArray.put(new File(getFileName(base64), MimeTypes.ContentType(FileExtension.getType(base64)), folder, convertUriToBase64(), null));
Log.d(TAG, JsonUtil.toJson(jsonArray));
And here is my model class:
public class File {
String fileName;
int fileType;
String fileFolder;
String base64String;
byte[] bytes;
public File(String fileName, int fileType, String fileFolder, String base64String, byte[] bytes){
this.fileName = fileName;
this.fileType = fileType;
this.fileFolder = fileFolder;
this.base64String = base64String;
this.bytes = bytes;
}
}
Any help will be useful thank you!
Upvotes: 0
Views: 799
Reputation: 4070
Don't mix JSON elements with your own model. Here's an example using Gson.toJson
which produces the expected result:
package test;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
public class GsonTest {
// Your model class
public static class Test {
private int x;
private int y;
public Test(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
List<Test> list = new ArrayList<>();
list.add(new Test(1, 2));
list.add(new Test(2, 3));
System.out.println(new Gson().toJson(list));
// output: [{"x":1,"y":2},{"x":2,"y":3}]
}
}
Upvotes: 1