简体   繁体   中英

Get dirrectly data in Json with Gson

I have a json string same:

{
   k1: v1,
   k2: v2,
   k3: {
     k31:{
        k32:{
          k33:{
             k34: v3
          }
        }
     }
   }
   k4: v4,
   k5: v5,
   k6: v6,
}

I create a object Object K = {k1, k2, k3, k4, k5, k6} and value get from abover string should be {v1, v2, v3, v4, v5, v6}

With Gson it is easy to get v1, v2, v4, v5, v6. With v3 , How to get it directly, I don't want make a object wrapper k33 , then k32 , then k31 , then k3 to get v3.

We also can't pass k31, k32, k33, k34 to get v3 as Jsoup
if @SerializedName struct as @SerializedName("k3.k31.k32.k33.k34").

Pls show me a solution for this case.

With Gson, ou can treat k3, and k31 through k34 as Json Arrays. K34 is then a Json Object to get the value for k34. Try this:

String  value = jsonObj.getAsJsonArray("k3").getAsJsonArray("k31")
.getAsJsonArray("k32").getAsJsonArray("k33").getAsJsonObject().get("k34");

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