-3

heyy. i have a problem with Json.

{"Status":"1","History":[{"Datetime":"2016-03-21 13:44:49","Mooble":"8745868526","Amount":"10.0000","Operator":"IDEA","ABPNo":"5000019864","OPTNO":"DL21032113440137","Status":"Success"},{"Datetime":"2016-03-20 16:59:20","Mooble":"7840802130","Amount":"10.0000","Operator":"IDEA","ABPNo":"5000019758","OPTNO":"DL21032016590062","Status":"Success"},{"Datetime":"2016-03-14 19:44:07","Mooble":"9911706716","Amount":"20.0000","Operator":"IDEA","ABPNo":"5000019034","OPTNO":"DL22031419440037","Status":"Success"},{"Datetime":"2016-03-14 19:00:03","Mooble":"9437445595","Amount":"10.0000","Operator":"BSNL TOPUP","ABPNo":"5000019023","OPTNO":"14230103343550","Status":"Success"}]}

this is the response which i got from server. i want to store this response into a String type array. like this - String[] Datetime = {"2016-03-20 16:59:20","2016-03-21 13:44:49","2016-03-14 19:44:07","2016-03-14 19:00:03"}; String[] Mooble = {"8745868526","7840802130","9911706716","9437445595"};

how can i store this response into this format. please help.

Prabal.PX
  • 33
  • 1
  • 11

2 Answers2

1

Using GSON library is one option (have a look in this example http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ )

But in sum:

  1. Create POJO's to deal with the json structure

MainHistory.java

public void MainHistory{
   @SerializedName("Status")
   private String status;

   @SerializedName("History")
   private List<ChildHistory> history;

   //getters and setters
}

ChildHistory.java

public void ChildHistory{
  @SerializedName("Mooble")
  private String mooble;

  @SerializedName("Amount")
  private String amount;

  //put the rest of the attributes

  //getters and setters
}
  1. Convert back to object.

    MainHistory obj = gson.fromJson(jsonResponse, MainHistory.class);

rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
0

Try this Logic..

if status == 1 

JSONArray history = response.getJSONArray("History");

for int i = 0 ; i < history.length()
JSONObject eachItem = history.getJSONObject(i);
datetime[i] = eachItem.getString("Datetime");
mooble[i] = eachItem.getString("Mooblle");

this is just a Pseudocode for you to understand.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49