首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取、修改和发布json对象?

如何获取、修改和发布json对象?
EN

Stack Overflow用户
提问于 2016-09-05 21:16:05
回答 1查看 157关注 0票数 0

正如我在3天前的旧话题中所提到的- Last Topic

我得到了一个json响应,并将其更改为字符串。Json响应表示一个用户对象。在User-Object中,我想搜索一个特定的项目并将其删除。在那之后,我想再次通过HttpPost发布它。

代码语言:javascript
复制
    private static String getContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity == null) return null;
BufferedReader reader;
try {
    reader = new BufferedReader(new InputStreamReader(entity.getContent()));
    String line = reader.readLine();
    reader.close();
    return line;
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

}

代码语言:javascript
复制
String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");

通过将属性保存在JsonArray中来搜索特定项目。

代码语言:javascript
复制
ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
for (int i = 0; i < projectsArray.length; i++) {
JSONObject current = projectsArray.get(i);
if (current.get("projectKey") == "**ProjectName**") {
indexesToRemove.add(i);
}
} 

正在删除项目...

代码语言:javascript
复制
for (int i = indexesToRemove.size()-1; i>=0; i--) 
{ 
projectsArray.remove(indexesToRemove.get(i));
}

这是完美的工作和我的搜索项目被删除。但问题是,我想通过HttpPost再次发布修改后的用户对象/字符串。我删除的项目只在我的JsonArray "projectsArray“中,而不是从一开始就在我的字符串中。我不能发布"projectsArray"....

代码语言:javascript
复制
        HttpPost UserChange = new HttpPost (TestUserURL+user);  //TODO: 
        UserChange.setHeader("Accept", "application/json");
        UserChange.setHeader("Content-type", "application/json");

        params = new StringEntity("ModifiedJsonString", HTTP.UTF_8);   // How do i get the complete Json string?
        UserChange.setEntity(params);

        HttpResponse UserChangeResponse = httpclient.execute(UserChange);

        HttpEntity entity2 = UserChangeResponse.getEntity();
            if (entity2 != null) {
                entity2.consumeContent();                       
            }

我需要"ModifiedJsonString",它包含从头开始的完整json文件。

代码语言:javascript
复制
params = new StringEntity(ModifiedJsonString, HTTP.UTF_8);

诚挚的问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-05 22:04:51

下面的代码移除一个选定的项目。

代码语言:javascript
复制
String jsonString = "{    \"account\": \"Kpatrick\",    \"firstname\": \"Patrick\",    \"instances\": [        {            \"id\": \"packerer-pool\",            \"key\": \"packerer-pool123\",            \"userAccount\": \"kpatrick\",            \"firstname\": \"Patrick\",            \"lastname\": \"Schmidt\"        }    ],    \"projects\": [        {            \"id\": \"packerer-projectPool\",            \"projectKey\": \"projectPool-Pool\",            \"cqprojectName\": \"xxxxx\"        },        {            \"id\": \"packerer-secondproject\",            \"projectKey\": \"projectPool-Pool2\",            \"cqprojectName\": \"xxxx\"        },        {            \"id\": \"packerer-thirdproject\",            \"projectKey\": \"projectPool-Pool3\",            \"cqprojectName\": \"xxxx\"        }    ],    \"clients\": [],    \"dbid\": 76864576,    \"version\": 1,    \"id\": \"dbpack21\"}";
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);

ArrayList<String> listOfNodes = new ArrayList<String>();
JSONArray projectArray = (JSONArray) jsonObject.get("projects");
int len = projectArray.size();
if (projectArray != null) {
    for (int i = 0; i < len; i++) {
        String projectId = ((JSONObject) projectArray.get(i)).get("projectKey").toString();
        if (!projectId.equals("projectPool-Pool2")) {
            listOfNodes.add(projectArray.get(i).toString());

        }

    }
}
// Remove the element from arraylist
// Recreate JSON Array
JSONArray jsArray = new JSONArray();
jsArray.addAll(listOfNodes);
jsonObject.remove(projectArray);
jsonObject.put("projects", listOfNodes);

System.out.println(jsonObject.toString());

例如,这将打印以下删除其中一个项目的JSON字符串。

有了它之后,您就可以使用它来创建一个StringEntity,然后在HTTPPost调用中使用它。希望能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39331454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档