JSON parsing using Gson for Java

前端 未结 11 2288
青春惊慌失措
青春惊慌失措 2020-11-22 04:57

I would like to parse data from JSON which is of type String. I am using Google Gson.

I have:

jsonLine = \"
{
 \"data\": {
  \"translati         


        
11条回答
  •  鱼传尺愫
    2020-11-22 05:15

        JsonParser parser = new JsonParser();
        JsonObject jo = (JsonObject) parser.parse(data);
        JsonElement je = jo.get("some_array");
    
        //Parsing back the string as Array
        JsonArray ja = (JsonArray) parser.parse(o.get("some_array").getAsString());
        for (JsonElement jo : ja) {
        JsonObject j = (JsonObject) jo;
            // Your Code, Access json elements as j.get("some_element")
        }
    

    A simple example to parse a JSON like this

    { "some_array" : "[\"some_element\":1,\"some_more_element\":2]" , "some_other_element" : 3 }

提交回复
热议问题