0

I want to implement my old code when I was populating JSON array into listview but my current json haven't specified array, overall json is an array...

Can somebody tell me how to transform my code to use JSON like this?

https://api-v2.hearthis.at/categories/drumandbass/?page=15&count=2

Code to use gson, my old:

protected List<Model> doInBackground(String... params) {
  HttpURLConnection connection = null;
  BufferedReader reader = null;
  try {
    URL url = new URL(params[0]);
    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    InputStream stream = connection.getInputStream();
    reader = new BufferedReader(new InputStreamReader(stream));
    StringBuffer buffer = new StringBuffer();
    String line ="";
    while ((line = reader.readLine()) != null){
      buffer.append(line);
    }

    String finalJson = buffer.toString();

    JSONObject parentObject = new JSONObject(finalJson);
    JSONArray parentArray = new JSONArray("arrayname");

    List<Model> dataModelList = new ArrayList<>();

    Gson gson = new Gson();
    for(int i=0; i<parentArray.length(); i++) {
      JSONObject finalObject = parentArray.getJSONObject(i);

      Model modelList = gson.fromJson(finalObject.toString(), Model.class);
      dataModelList.add(modelList);
    }
    return dataModelList;
  } catch (MalformedURLException e) {
    e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } catch (JSONException e) {
      e.printStackTrace();
  } finally {
    if(connection != null) {
      connection.disconnect();
    }
    try {
      if(reader != null) {
        reader.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 return  null;
 }
0

2 Answers 2

1

Instead of doing this:

JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray("arrayname");

Just do this:

JSONArray parentArray = new JSONArray(finalJson);
Sign up to request clarification or add additional context in comments.

1 Comment

I figured out it before you but thx, answer accepted because it's 100% correct
1

If i understand the problem correctly, i guess you are having trouble parsing the array who's name is not specified with a key. So, a simple but not a very good way of doing this would be, when you create your finalJson like this:

String finalJson = buffer.toString();

just add a line to check if it contains an array opening bracket, like this:

if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
  "arrayname": [");
}

also, close the array appropriately.

if(finalJson.contains("]")){
finalJson = finalJson.replace("]","]
}");
}

This way, u will have a name for the array to parse, and i guess this is what you are looking for. But this approach might fail if you have more than 1 array in your Json, in that case you will have to use startsWith & endsWith string methods to give a name to your array. But, as of now, from what your Json looks like, this would work.

Comments

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.