-3

I don't know how to get an array from a key value. Emails can have more than one value so how can I parse it as emails is not a JsonArray.

This is my response:

{
    "userid": "377c8a214c",
    "username": "princek082+skip@gmail.com",
    "emails": [
        "princek082+skip@gmail.com",
        "upendrasinghktp@gmail.com"
    ],
    "termsAccepted": "2016-03-03T10:52:14+05:30",
    "emailVerified": false
}
mort
  • 12,988
  • 14
  • 52
  • 97

2 Answers2

1

As Shree Krishna has answered already you can get JSONArray from email key then use some loop to get values and store it to your collections. But if json if large then parsing is a mess so i will suggest you to make use of GSON where you can generate java POJO classes based on your json keys then process your object to show in UI.for example in your case declare like below-

private String userid;
private String username;
private List<String> emails = new ArrayList<String>();
private String termsAccepted;
private Boolean emailVerified;

create Getter Setter and your are done.

Godfather
  • 833
  • 9
  • 14
  • Bro I am sorry that OP also changed your answer's from checked to unchecked... Not to worry yor answer is also very useful.. – Shree Krishna Apr 19 '16 at 15:43
0

Parse like this,

ArrayList<String> myEmailArray = new ArrayList<>();

JSONObject  jsonObj = new JSONObject(yourJson);
JSONArray jsonEmailArray = jsonObj.getJSONArray("emails");
for(int i=0; i<=jsonEmailArray.length(); i++){
     myEmailArray.add(jsonEmailArray.getString(i));
}

Please let me know if any mistakes are there, Because it's directly written in editor, not tested.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • @UpendraSinghChauhan pleasure to hear. – Shree Krishna Apr 19 '16 at 06:42
  • @UpendraSinghChauhan Why did you uncheck this ? Is there any problem you got ? Changing the correct answer or unchecking it, OR not marking correct even after getting right answer may cause you not getting the answers in future, Because most users will see if you have habit of responding to answers or not before answering your question. That answer is an Extra solution you can achieve your result with. But you told about parsing not about converting it as POJO using another library. – Shree Krishna Apr 19 '16 at 11:12
  • Actually this is my 1st question on stack. So, I don't know about that. I will take care of these things from now onwards. Yeah, your answer is totally correct and helpful. I implemented your way in my code. – Upendra Singh Chauhan Apr 19 '16 at 15:04
  • Bro I suggest you taking a tour, about community, why votes are important, why privileges and etc... There are alot things to know about... And welcome to stack overflow... – Shree Krishna Apr 19 '16 at 15:45