0

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!

6
  • Have you tried jsonArray.toString()? Commented Aug 24, 2021 at 20:22
  • @digitalbreed Yes and it doesn't return a valid json instead it returns a ["com.package.name.File@d917cac"] Commented Aug 24, 2021 at 20:39
  • Well, you put a File into it, which is not a valid JsonElement. Commented Aug 24, 2021 at 20:41
  • @digitalbreed 'File' is a custom model class. Commented Aug 24, 2021 at 20:46
  • @digitalbreed I updated the question so you know what I mean Commented Aug 24, 2021 at 20:47

1 Answer 1

1

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}]

    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

it works perfectly thank you, if I may ask how would one go about get lets say 2-10 items in a list. Like for example when you create a post on Facebook you can add a set amount of images to your post how would you go about getting all those images data and adding it an ArrayList?
@Tihan-NicoPaxton not sure what you mean with "getting all those images data", but if you are asking about building a similar process, with file selection, preview, cropping, uploading, storage, security,... that's probably a bit too much to answer in a single SO question.
Let's say you wanna post multiple images and upload it to a storage service, how would you go about it getting all those images data and adding it into the Json Arraylist. For example: 'List<Post> posts = new ArrayList(); posts.add(new Post("data"));' but instead of adding add whole time how would you get all them combined and add it as one, if that makes sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.