7

I have the following json that I need to upload:

[{"key":"value","key1":"value1"},
[{"innerkey":"innervalue","filename":"name"},{"innerkey":"innervalue","filename":"name"}]]

the two innerkeys which are inside the JsonArray have two files which need to be uploaded.

I am using okhttp multipart to upload them.

A normal field is added as

multipart.addFormDataPart(key, value);

and a file is added as

 multipart.addFormDataPart("filename", "image.jpeg", RequestBody.create(MediaType.parse("image/jpeg"), new File(path)));

I have uploaded a non nested json before by converting the json into a hashmap and adding the parts using a for loop

 MultipartBody.Builder multipart = new MultipartBody.Builder();
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if(!key.equals("filePath")){
                multipart.addFormDataPart(key, value);

            }else{

                String filename = "";
                String type = MyUtility.getMimeType(key);
                filename = key.substring(key.lastIndexOf("/") + 1);
                multipart.addFormDataPart("fileName", filename, RequestBody.create(MediaType.parse(type), new File(key)));
            }
        }

the new structure seems to be quite difficult to process how do I do it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pemba Tamang
  • 458
  • 3
  • 16
  • Hi, So you want to upload multiple files with some value. Right? – Pratik Butani Feb 06 '19 at 04:42
  • yes but it with a nested json – Pemba Tamang Feb 06 '19 at 04:44
  • I don't think such desired format is possible in **MultiPart POST** method, just imagine how you can share such data using **postman app**. One solution would be passing **file data** as **Base64 encoded string** and passing entire data using raw request body. – Jeel Vankhede Feb 06 '19 at 05:12
  • Check this answer https://stackoverflow.com/a/35866301/10271334, this might help for your case though. – Jeel Vankhede Feb 06 '19 at 05:25
  • I think that this is a lost cause could anyone look at this question, if it's ok by the community rules I am willing to give the bounty if someone answers this as well. https://stackoverflow.com/questions/54529577/how-to-add-a-popup-menu-with-a-customlayout-with-pointer-arrow-and-anchor-it-to?noredirect=1#comment95860718_54529577 – Pemba Tamang Feb 07 '19 at 14:40
  • Try using the Ion library mentioned in this answer https://stackoverflow.com/questions/31488511/how-to-post-multiple-image-files-using-ion-library – Abilash Feb 12 '19 at 06:45

1 Answers1

0

Please tell me you're looking for something like this

    public static Map<String,String> convertToMap(Object yourJsonArrayOrJsonObject) throws Exception
    {
        HashMap<String, String> map = new HashMap<>();
        processJSON(null, yourJsonArrayOrJsonObject, map);
        return map;
    }

    private static void processJSON(String jsonKey, Object jsonItem, Map<String, String> map)
    {
        if (jsonItem instanceof JSONArray)
        {
            JSONArray array = (JSONArray) jsonItem;
            for (Object arrayItem : array)
            {
                processJSON(null, arrayItem, map);
            }
        } else if (jsonItem instanceof JSONObject)
        {
            JSONObject json = (JSONObject) jsonItem;
            Set<String> keys = json.keySet();
            for (String key : keys)
            {
                processJSON(key, json.get(key), map);
            }
        } else
        {
            map.put(jsonKey, String.valueOf(jsonItem));
        }
    }