简体   繁体   中英

Parsing JSON array

I'm using GSON for parsing JSON response.

Unfortunately the WebApi on the server has quite untypical JSON objects.

I need to parse Attachments array from this JSON (there can be more attachments):

{"htmlMessage":"text","Attachments":{"8216096_0":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg","contentDisposition":"attachment","size":86070}}}

Where 8216096_0 is attachments id.

I can't do it with Gson (or I don't know how) so I'm trying to do it with JSONObjects:

// parse attachments
JSONObject attachmentsJson = result.getJSONObject("Attachments");

Then I have one JSONObject with an array of attachments, but I don't know how to get them to the ArrayList from JSONObject because the key value isn't static but generated id..

Thank you


Thanks to all guys for helping! My final solution looks like this especially thanks to @Jessie A. Morris and his final answer!

List<AttachmentModel> attachmentsList = new ArrayList<AttachmentModel>();
for( Map.Entry<String, JsonElement> attachment : attachments.entrySet()) {
    AttachmentModel attachmentModel = new AttachmentModel();
    attachmentModel = gson.fromJson(attachment.getValue().getAsJsonObject().toString(), AttachmentModel.class);;
    attachmentModel.setmUid(attachment.getKey());
    attachmentsList.add(attachmentModel);
 }

Okay, I've changed my example a little bit and am certain that this does work correctly (I just tested it):

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jessie on 14-07-09.
 */
public class TestGson {
    private static String JSON = "{\"htmlMessage\":\"text\",\"Attachments\":{\"8216096_0\":{\"content\":null,\"filename\":\"plk.jpg\",\"contentType\":\"image/jpeg\",\"contentDisposition\":\"attachment\",\"size\":86070}}}\n";

    public static void main(String[] args) {
        JsonObject json = new JsonParser().parse(JSON).getAsJsonObject();
        JsonObject attachments = json.getAsJsonObject("Attachments");

        List<JsonObject> attachmentsList = new ArrayList<JsonObject>();
        for( Map.Entry<String, JsonElement> attachment : attachments.entrySet()) {
            attachmentsList.add(attachment.getValue().getAsJsonObject());
        }

        System.out.println("attachmentsList at the end? " + attachmentsList);
    }
}

I'm not completely sure if this really works:

final Map<String,JSONObject> attachmentsJson = (Map<String,JSONObject>) jsonArray.getJSONObject("Attachments");
for(String attachmentId : attachmentsJson.keySet()) {
  final JSONObject attachmentJson = attachmentsJson.get(attachmentId);
}

The "Attachments" obj in your example is not an array.

Json arrays are denoted by [....].

"Attachments" is a Json object holding an inner object called "8216096_0".

so to get the inner values do as follows:

JSONObject attachmentsJson = result.getJSONObject("Attachments");
JSONObject inner = attachmentsJson.getJSONObject("8216096_0");

// and interrogate the inner obj:
String content = inner.getString("content");
String filename = inner.getString("filename");


Finally, and for example sake, I will add the code for processing a (real) Json array :

{"htmlMessage":"text",
   "Attachments":[{"8216096_0":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg",
                  "contentDisposition":"attachment","size":86070}},                  
                 {"8216096_1":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg",
                    "contentDisposition":"attachment","size":86070}},  
              ]
   }


It will go like this:

JSONArray attachmentsJson = result.getJSONObject("Attachments");
int len = attachmentsJson.length();
for (int i = 0; i < len; i++) {
    JSONObject elem =  attachmentsJson.getJSONObject(i); // <------ get array element 
    JSONObject inner = elem.getJSONObject("8216096_0");   
    // and interrogate the inner obj:
    String content = inner.getString("content");
    String filename = inner.getString("filename");
}

..Or similar, depending on your Json's exact format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM