Java 将对象GSON放入列表中

Java 将对象GSON放入列表中,java,android,json,gson,Java,Android,Json,Gson,我想在将JSON数组填充到listview中时实现我的旧代码,但我当前的JSON没有指定数组,整个JSON是一个数组 有人能告诉我如何将代码转换成这样使用JSON吗 使用gson的代码,我的旧版本: protected List<Model> doInBackground(String... params) { HttpURLConnection connection = null; BufferedReader reader = null; try { URL

我想在将JSON数组填充到listview中时实现我的旧代码,但我当前的JSON没有指定数组,整个JSON是一个数组

有人能告诉我如何将代码转换成这样使用JSON吗

使用gson的代码,我的旧版本:

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;
 }
protectedlist doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(行);
}
字符串finalJson=buffer.toString();
JSONObject parentObject=新的JSONObject(finalJson);
JSONArray parentArray=新的JSONArray(“arrayname”);
List dataModelList=新的ArrayList();
Gson Gson=新的Gson();

对于(int i=0;i如果我正确理解了这个问题,我猜您在解析数组时遇到了问题,因为数组的名称没有用键指定。因此,一个简单但不是很好的方法是,当您创建finalJson时,如下所示:

String finalJson = buffer.toString();
if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
  "arrayname": [");
}
只需添加一行来检查它是否包含数组开口括号,如下所示:

String finalJson = buffer.toString();
if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
  "arrayname": [");
}
另外,适当地关闭阵列

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

这样,u将有一个要解析的数组的名称,我想这就是您要寻找的。但是,如果Json中有多个数组,这种方法可能会失败,在这种情况下,您必须使用startsWith&endsWith字符串方法为数组命名。但是,从现在的Json外观来看,这是可行的。

如果我正确地理解了这个问题,我猜您在解析没有用键指定名称的数组时遇到了问题。因此,一个简单但不是很好的方法是,当您创建finalJson时,如下所示:

String finalJson = buffer.toString();
if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
  "arrayname": [");
}
只需添加一行来检查它是否包含数组开口括号,如下所示:

String finalJson = buffer.toString();
if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
  "arrayname": [");
}
另外,适当地关闭阵列

if(finalJson.contains("]")){
finalJson = finalJson.replace("]","]
}");
}
这样,u将有一个要解析的数组的名称,我想这就是您要寻找的。但是,如果Json中有多个数组,这种方法可能会失败,在这种情况下,您必须使用startsWith&endsWith字符串方法为数组命名。但是,从现在的Json外观来看,这是可行的。

不要这样做:

JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray("arrayname");
只要这样做:

JSONArray parentArray = new JSONArray(finalJson);
而不是这样做:

JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray("arrayname");
只要这样做:

JSONArray parentArray = new JSONArray(finalJson);

我在你之前就知道了,但是thx,答案被接受,因为它是100%正确的我在你之前就知道了,但是thx,答案被接受,因为它是100%正确的