【问题标题】:Add json object to existing json array将 json 对象添加到现有的 json 数组
【发布时间】:2011-09-20 15:31:49
【问题描述】:

您好,我想通过添加新对象来更新现有的 json 数组。如何在 Android 中做到这一点。

我的 json 位于远程机器上,我正在使用 GSON 解析 JSON。

提前致谢

【问题讨论】:

    标签: android json gson


    【解决方案1】:
            DefaultHttpClient client = new DefaultHttpClient(); 
            String finalJson="";
            HttpGet getRequest = new HttpGet(url);
    
            try {
    
               HttpResponse getResponse = client.execute(getRequest);
    
               final int statusCode = getResponse.getStatusLine().getStatusCode();
    
               if (statusCode != HttpStatus.SC_OK) { 
                  Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); 
                  return null;
               }
    
               HttpEntity getResponseEntity = getResponse.getEntity();
               finalJson=EntityUtils.toString(getResponseEntity);
               System.out.println("getResponse.."+s);
    
    
            } 
            catch (IOException e) {
               getRequest.abort();
               Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
            }
    
            finalJson=finalJson.substring(source.indexOf("[")+1)+"\"object:\"" ; // your logic to append object
            Gson gson = new Gson();
    
            parseJsonClass response = gson.fromJson(source, parseJsonClass .class);
    

    希望你能明白我的意思

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多