1

I have a contacts which is in the form of JSON. Now I want to decode them into String array. There are two arrays; names and phones. I'm using this code:

 String[] names;
 String[] phones;
 String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
 try {
 JSONArray jsonArray = new JSONArray(test);
 JSONObject jsonObject = new JSONObject(jsonArray.toString());
 Log.i("INFO", String.valueOf(jsonObject.length()));
 } catch (JSONException e) {
 e.printStackTrace();
 }

This generates an error. How can I add all names in names array and all phones in phones array. Like names[0] is assigned A which is a first name and phones[0] assigned 911 which is first phone number corresponding to first name. How can I do that, I'm new in android?

Biffen
6,3776 gold badges32 silver badges38 bronze badges
asked Feb 21, 2017 at 5:42
4
  • 1
    What is new JSONObject(jsonArray.toString()) supposed to achieve?! Commented Feb 21, 2017 at 5:47
  • I think there is objects within array So, I try to retrieve these. Like there are three objects, isn't it ? Commented Feb 21, 2017 at 5:48
  • you have to fetch each object by lopping through it , you can't simply get it like that Commented Feb 21, 2017 at 5:49
  • @PakDeveloper Yeah, so why are you trying to turn the array into one object?! Commented Feb 21, 2017 at 5:49

3 Answers 3

3

The problem is in this line

JSONObject jsonObject = new JSONObject(jsonArray.toString());

you are trying to convert JSONArray in JSONObject , which you can't. if you want to access JSONObject inside of JSONArray you have to loop through each, or you can get specific object by it's index.

 String[] names;
 String[] phones;
 String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
 try {
 JSONArray jsonArray = new JSONArray(test);
 phones = names = new String[jsonArray.length()];
 for(int i = 0 ; i < jsonArray.length(); i ++){
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 names[i] = jsonObject.getString("name");
 phones[i] = jsonObject.getString("phone");
 Log.i("INFO", "name : " + jsonObject.getString("name") + " , phone : " + jsonObject.getString("phone"));
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
answered Feb 21, 2017 at 5:48
Sign up to request clarification or add additional context in comments.

1 Comment

This answer looks perfect .
1

You can't convert json arrya to json object like that:

try {
 JSONArray jsonArray = new JSONArray(test);
 for(int i=0; i<jsonArray.length(); i++) {
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 // Your code goes here.. 
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
answered Feb 21, 2017 at 5:48

Comments

0

here is the required code.

 ArrayList<String> names = new ArrayList<>();
 ArrayList<String> phones = new ArrayList<>();
 String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
 try {
 JSONArray jsonArray = new JSONArray(test);
 for (int i = 0; i < jsonArray.length(); i++) {
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 String name = jsonObject.getString("name");
 String phone = jsonObject.getString("phone");
 names.add(name);
 phones.add(phone);
 Log.i("INFO", name + ", " + phone);
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }

above you have used simple arrays they can store data only of fixed size. But arrayList and List can store data of variable size or you do not need to fix their size.

answered Feb 21, 2017 at 5:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.