0

I'd like to firstly apologize for my use of terminologies as I am fairly new to working with Json objects and arrays.

I have searched for various examples where I can extract data from a Json object, but none are similar to the situation I am in. I am attempting to extract the string "Plan A (Economy Plan)", "Plan B (Deluxe Plan)", and "Plan C (Ramadan)" from the following Json:

{
"status": "success",
"data": {
    "1:Plan A (Economy Plan)": [{
        "idscheme": "49",
        "plan_name": "1",
        "plan_id": "ABS-772",
        "term": "8",
        "payment": "1000",
        "amount": "8000",
        "expected_amount": null,
        "added_by": "2",
        "updated_on": "2017-09-27"
    }, {
        "idscheme": "50",
        "plan_name": "1",
        "plan_id": "ABS-773",
        "term": "8",
        "payment": "2000",
        "amount": "16000",
        "expected_amount": null,
        "added_by": "1",
        "updated_on": "2017-09-28"
    }, {
        "idscheme": "48",
        "plan_name": "1",
        "plan_id": "ABS-762",
        "term": "7",
        "payment": "4000",
        "amount": "28000",
        "expected_amount": null,
        "added_by": "1",
        "updated_on": "2017-09-28"
    }],
    "2:Plan B (Deluxe Plan)": [{
        "idscheme": "33",
        "plan_name": "2",
        "plan_id": "ABS-486",
        "term": "8",
        "payment": "2000",
        "amount": "16000",
        "expected_amount": null,
        "added_by": "2",
        "updated_on": "2017-09-27"
    }, {
        "idscheme": "47",
        "plan_name": "2",
        "plan_id": "ABS-831",
        "term": "7",
        "payment": "3000",
        "amount": "21000",
        "expected_amount": null,
        "added_by": "2",
        "updated_on": "2017-09-27"
    }, {
        "idscheme": "46",
        "plan_name": "2",
        "plan_id": "ABS-776",
        "term": "7",
        "payment": "4000",
        "amount": "28000",
        "expected_amount": null,
        "added_by": "2",
        "updated_on": "2017-09-27"
    }],
    "5:Plan C (Ramadan)": [{
        "idscheme": "51",
        "plan_name": "5",
        "plan_id": "ABS-865",
        "term": "7",
        "payment": "3000",
        "amount": "21000",
        "expected_amount": null,
        "added_by": "42",
        "updated_on": "2017-10-15"
    }, {
        "idscheme": "52",
        "plan_name": "5",
        "plan_id": "ABS-389",
        "term": "8",
        "payment": "4000",
        "amount": "32000",
        "expected_amount": null,
        "added_by": "42",
        "updated_on": "2017-10-15"
    }, {
        "idscheme": "53",
        "plan_name": "5",
        "plan_id": "ABS-451",
        "term": "8",
        "payment": "5000",
        "amount": "40000",
        "expected_amount": null,
        "added_by": "42",
        "updated_on": "2017-10-15"
    }]
},
"plancount": 3

}

The idea is that this Json object is created from the use of an API, and I want to dynamically put the different "plans" in a spinner in Android. Currently I have hard-coded the values into the spinner, but that number of plans can change and I don't want to hard-code it each time.

The format of the Json object is a little different than usual since the API's were built in such a way, is there a way to extract the different plans instead of having to change the API since that would require some more work?

Below are the steps I have followed to achieve this:

  1. String APIUrl = "url for API"
  2. I am sending the APIUrl along with the necessary data to a class DataAPI which extends AsyncTask and sends an Http POST request.
  3. The server response in stored in a String JsonResult.
  4. I have then created a jsonObject from the string and checked for whether jsonObject.getString("status").equals("success")
  5. It is here that I am getting stuck because I don't know if there is a way to extract the required strings ("1:Plan A (Economy Plan)", "2:Plan B (Deluxe Plan)", and "3:Plan C (Ramadan)") so I can split ("Plan A (Economy Plan)", "Plan B (Deluxe Plan)", and "Plan C (Ramadan)") and place into the Android spinner.

Thanks you very much for your help, and if you'd like any changes or things I need to include in the post, please let me know.

LS_
  • 6,763
  • 9
  • 52
  • 88
Abd Maj
  • 197
  • 1
  • 13
  • Since the number of plans can change you can't convert that json to pojo, I think you should be using a `Map`, you can look at an example similar to your question [here](https://stackoverflow.com/questions/47188934/how-to-turn-this-json-response-into-pojo) – LS_ Nov 09 '17 at 10:19

4 Answers4

0

You can use gson for that purpose:

Gson gson = new Gson();
YourPojoOfJson yourObjectOfPojo = gson.fromJson(jsonObject,YourPojoOfJson .class);

And to make pojo of your json go to: http://www.jsonschema2pojo.org/

Copy your json there and select json from right panel and none. After that you have to click on preview it will show several classes.Creste these classes to make object of your json. Once you have object of your class then yu can get any value anywhere by just calling object and by using get property.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
0

You can use Retrofit (http://square.github.io/retrofit/) for sending async HTTP POST request and convert response to pojo.

Example:

@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);
Alexey Zatsepin
  • 1,179
  • 7
  • 14
0

i don't have enough reputation to write comment so i will tell you my note :

"data" must be jsonArray to achieve what you want so you can iterate on it simply but "data" is now jsonObject and has this three plan and you must know what key of each of them. In short make "data" JsonArray insteadOf jsonObject or the one who gave you this API tell him to change it and it will be easy to iterate on it

Mosa
  • 353
  • 2
  • 16
0

Much appreciate the response guys. I'd like to post what I ended up doing because I'm inexperienced with Gson, Pojo, Retrofit, etc. Hopefully I'm going to dig deeper and learn how to work with these things. Anyway, I converted the Json object to a string and then used the Pattern compiler to extract the strings that I needed. I then put these in an ArrayList called plans.

String temp = jsonObject.getJSONObject("data").toString();

Pattern p = Pattern.compile("\"(.*?)\":\\[");
Matcher m = p.matcher(temp);

m.find();

plans.add(m.group(1));

p = Pattern.compile("],\"(.*?)\":\\[");
m = p.matcher(temp);

while (m.find()){

    plans.add(m.group(1));

}

status = true;

return plans;

This works perfectly if more plans were to be added hence it's a workaround I'm sticking with till I figure out a better way.

Abd Maj
  • 197
  • 1
  • 13