-3
{
    "status": true,
    "data": {
        "1": "Business People",
        "2": "Actors",
        "3": "Musicians",
        "4": "Sports People",
        "5": "Artists",
        "6": "Politicians"
    },
    "message": "Get data successfully."
}

I want to parse the above json.

Thanks in Advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vikram Kaldoke
  • 150
  • 1
  • 8
  • Many ways to do it. You can use javas integrated json api or some external library (jackson, gson etc) – NiVeR Apr 15 '19 at 13:02
  • so far what you have done.whether you ar getting it from Api call or something else – primo Apr 15 '19 at 13:02
  • https://www.tutorialspoint.com/android/android_json_parser.htm – Carnal Apr 15 '19 at 13:03
  • Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – ADM Apr 15 '19 at 13:19

5 Answers5

1

I think it's not a good practice to keep numbers as key in your object, I advise you to use a JsonArray

{
 "status":true,
 "message":"Get data successfully.",
 "data":[ "Business People", "Actors", "Musicians", "Sports People", "Artists" 
 ,"Politicians"]
}

and then use JsonArray to parse data

Ahmed.ess
  • 265
  • 3
  • 10
0

Change your data to JSONArray as a set of JSONObject to perform dynamic job

 JSONObject json = new JSONObject(your_string)
 JSONArray  data = json.getJsonArray("data");

 for(int i = 0;i<data.length();i++)
  {
   JSONObject item = data.getJSONObject(i);
   String itemString = item.getString(Integer.parseInt(i+1));
 /// do whatever you want ie : add itemString to a list
  }
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
0

You may use handy service that generates you java model.

Then just use Gson to serialize-deserialize json.

However in your case that "1", "2", "3" etc may be fixed parameters, or dynamic Map.

0

I suggest using Klaxon to parse the JSON https://github.com/cbeust/klaxon

Really easy usage:

Klaxon().parse<yourClass>(yourJsonAsString)

where yourClass is a data class with your parsing needs. See Klaxon documentation with for each of your needs. Comment if you need help.

0

Visit this website https://quicktype.io/ it will help you in the creation of java models.

Now serialize and deserialize JSON by using GSON.

Add the following dependency implementation 'com.google.code.gson:gson:2.8.5'

Here is an example:

public Car fromJson(){

String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
Gson gson = new Gson();
Car car = gson.fromJson(json, Car.class);
return car;

}