1

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.

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • i think you must traverse to get the object.You can't get the v3 directly.Only XML have such a functionality. – Vishwa Jan 22 '16 at 05:00
  • Possible duplicate of [How to parse json string in Android?](http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android) – Jaiprakash Soni Jan 22 '16 at 05:08
  • Sorry, I need a solution with Gson to dirrectly parse, not how to parse generally. – mdtuyen Jan 22 '16 at 05:12
  • If is easy if Gson have @SerializedName struct as @SerializedName("k3.k31.k32.k33.k34"). But it has not – mdtuyen Jan 22 '16 at 05:13
  • study this and do some code so we can help you: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html – Suhas Bachewar Jan 22 '16 at 05:22
  • @SuhasB Please read my question clearer. I'm not started with Gson. Here, I want get v3 dirrectly. – mdtuyen Jan 22 '16 at 05:28

1 Answers1

1

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");
Gaspar
  • 159
  • 7