简体   繁体   中英

JSON Object GSON into list

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;
 }

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.

Instead of doing this:

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

Just do this:

JSONArray parentArray = new JSONArray(finalJson);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM