将json对象添加到现有的json数组

时间:2011-09-20 15:31:50

标签: android json gson

您好我想通过添加新对象来更新现有的json数组。如何在Android中完成。

我的json正躺在远程计算机上,我使用GSON来解析JSON。

先谢谢

1 个答案:

答案 0 :(得分:0)

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

从希望你能明白我的意思