0

First thanks to Pentium10 for this answer! it got me one step further.

I have a JSONArray which is generated in php by

echo (json_encode($output));

which generates this output

// linebreaks only for readability, they do not exist in the response
[["Fitch's Chemist","731 Hay St","(08) 9321 6411"],
["Ferrara Karaoke Bar","67 Milligan Street","(08) 9481 1909"],
["Target: Perth","712-720 Hay St","(08) 9327 3700"],
["C Restaurant","44 St Georges Tce","(08) 9220 8333"],
["Celona Joe Tailors","146 Murray St","(08) 9325 8274"],
["Fashion In Colour","Shop 2, 138 Barrack St","(08) 9218 8233"],
["Mainpeak","858 Hay St","(08) 9322 9044"],
["Fj Storen Painting Contractors","34 Queen St","(08) 9486 9292"],
["Financial Pathfinders","Level 4\/81 St Georges Tce","(08) 9213 9699"],
["Seasons of Perth","37 Pier St","(08) 9325 7655"],
["David Jones","622 Hay St","(08) 9210 4000"],
["Pharmacity Chemist Supermart","717 Hay St","(08) 9322 6921"],
["Austcare","10 Pier St","(08) 9325 9330"],
["Western Australia","8 Pier St","(08) 9261 6222"],
["Oceanic Cruises","5 Barrack","(08) 9325 1191"]]

This outputs the list array filled as follows;

list(0)["Fitch's Chemist","731 Hay St","(08) 9321 6411"]
list(1)["Ferrara Karaoke Bar","67 Milligan Street","(08) 9481 1909"]

what I now need to do is extract this further so that the "" enclosed data is stored in three separate arrays

    try {
        JSONArray jsonArray = new JSONArray(response);
        List<String> list = new ArrayList<String>();
        for (int i=0; i<jsonArray.length(); i++) {
            list.add( jsonArray.getString(i) );
            JSONArray jsonList = new JSONArray(list);
            BusinessName.add(jsonList.getString(0));
            Address.add(jsonList.getString(1));
            TelNo.add(jsonList.getString(2));
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

Thanks Martin

3
  • could you show the code that prints the list(0)... you pasted? Commented May 31, 2011 at 11:40
  • does the code work you added to your question? You should be a bit more communicative :) Commented May 31, 2011 at 12:26
  • Sorry You are right. Please be patient it is my first post. No the code doesn't work it throws a JSON error int out of range Commented May 31, 2011 at 12:43

2 Answers 2

1

Let's say you have a JSONArray variable called jsonArray and that it contains your data.

To extract the data, what you need to do it use:

jsonArray.getJSONObject(int index) (returns a JSONObject) - Use to get an object inside the object jsonArray.getJSONArray(int index) (returns a JSONArray) - use to get an array inside an array

the rest is self explanatory.

jsonArray.get(int index) (returns an Object) jsonArray.getBoolean(int index) (returns a Boolean) jsonArray.getDouble(int index) (returns a Double) jsonArray.getInt(int index) (returns an Integer) jsonArray.getLong(int index) (returns a Long) jsonArray.getString(int index) (returns a String)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks DallaRosa can you tell me which one I have is it an qbject or an array.
what you have there is an array of arrays. you use getJSONArray(number) to get the inner array and then use getString(number) on this second array to get each of the values
0

edit: I tried your example and the code nearly works but has one little mistake. You shouldn't use list to get the JSONArray jsonList.

This works (tested):

// this string is just used for testing, use your response...
String json = "[[\"Fitch's Chemist\",\"731 Hay St\",\"(08) 9321 6411\"],"
            + "[\"Ferrara Karaoke Bar\",\"67 Milligan Street\",\"(08) 9481 1909\"],"
            + "[\"Target: Perth\",\"712-720 Hay St\",\"(08) 9327 3700\"],"
            + "[\"C Restaurant\",\"44 St Georges Tce\",\"(08) 9220 8333\"],"
            + "[\"Celona Joe Tailors\",\"146 Murray St\",\"(08) 9325 8274\"],"
            + "[\"Fashion In Colour\",\"Shop 2, 138 Barrack St\",\"(08) 9218 8233\"],"
            + "[\"Mainpeak\",\"858 Hay St\",\"(08) 9322 9044\"],"
            + "[\"Fj Storen Painting Contractors\",\"34 Queen St\",\"(08) 9486 9292\"],"
            + "[\"Financial Pathfinders\",\"Level 4/81 St Georges Tce\",\"(08) 9213 9699\"],"
            + "[\"Seasons of Perth\",\"37 Pier St\",\"(08) 9325 7655\"],"
            + "[\"David Jones\",\"622 Hay St\",\"(08) 9210 4000\"],"
            + "[\"Pharmacity Chemist Supermart\",\"717 Hay St\",\"(08) 9322 6921\"],"
            + "[\"Austcare\",\"10 Pier St\",\"(08) 9325 9330\"],"
            + "[\"Western Australia\",\"8 Pier St\",\"(08) 9261 6222\"],"
            + "[\"Oceanic Cruises\",\"5 Barrack\",\"(08) 9325 1191\"]]";

try {
    JSONArray jsonArray = new JSONArray(json);
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < jsonArray.length(); i++) {
        list.add(jsonArray.getString(i));
        // thats the correct line:
        JSONArray jsonList = new JSONArray(jsonArray.getString(i));
        // Used for debug/test, use your own objects BusinessName, Address and TelNo
        System.out.println(jsonList.getString(0) + ":" + jsonList.getString(1) + ":" + jsonList.getString(2));
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

3 Comments

@WarrenFaith sorry I dont follow how this will help
It's just iterating through the json array.
@WarrenFaith Thanks I can see where I went wrong. I was trying to read from the whole list and not list(i). On the first iteration list(0) exists but list(1) does not hence the error. With your code I don't need list.add(jsonArray.getString(i));

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.