1


I get a server response like this:

{  
   "action":"add",
   "domain":"dm1",
   "params":{  
      "add1":"v1",
      "add2":"v2",
      "add3":"v3"
   }
}

And i have many domain (15) and each domain have multi action, and key in value of "params" key ("add1", "add2",...) that are depended on value of action value ("add", "remove", ...). So, i want to ask that, how can i get param value from server then push in a Model using Gson. I'm Android.
Thanks in advance!

TedVN
  • 536
  • 5
  • 10
  • how many values in `params`? Give more example to make it clear. And did you try it? Paste your code if you did. – Harry T. Jan 23 '17 at 02:41
  • @TruongHieu, value of params is json, and number key in that json is not fixed. It is depended on "action" value. I'm confused and i don't know how to resolve it. – TedVN Jan 23 '17 at 03:09

2 Answers2

0

I am not pretty sure. What your goal is. But You can do as below

{  
   "action":"add",
   "domain":"dm1",
   "params":[
    { "add":"v1" },
    { "add":"v2" },
    { "add":"v3" }    
   ]
}
Ali Safdar
  • 36
  • 3
0

You can parse your params key as a JSONObject and iterate through all its keys to get the values of those dynamic keys. Use JSONObject.keys to get an Iterator for keys. This answer should further help you - How to parse a dynamic JSON key in a Nested JSON result?

Community
  • 1
  • 1
Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44