java—更新jsonnode中每个项的值(jackson)

mhd8tkvw  于 2021-06-26  发布在  Java
关注(0)|答案(4)|浏览(635)
JsonNode response = objectMapper.readTree(responseBody);
String textData = response.get("data").toString();

下面是textdata的输出。

[{"acc":"1278915","for":"159000"},{"acc":"8963258","for":"152863"},{"acc":"9632578","for":"258963"}]

正如您在上面代码中看到的注解一样,textdata会像上面一样向我返回值。我想做上面的事 acc 那么,我该怎么做呢?
“acc”:“xx78915”,我想替换每个项目acc的前两位数字。
我有以下用途,但它不做。

String output = "xxxx" + textData.substring(4);

在那之后我回到我的视野

return (List<Map<String, String>>) objectMapper.readValue(text, List.class);
5lwkijsr

5lwkijsr1#

如果您需要为每个项目替换acc的前两位数字,请尝试regex:

String output = textData.replaceAll("(\"acc\":\"[0-9]{2})", "\"acc\":\"xx");
kd3sttzy

kd3sttzy2#

使用org.json库的示例-也许这对其他人有用

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {
    public static void main(String[] args) throws JSONException {
        JSONArray array = new JSONArray("[{\"acc\":\"1278915\",\"for\":\"159000\"},{\"acc\":\"8963258\",\"for\":\"152863\"},{\"acc\":\"9632578\",\"for\":\"258963\"}]");
        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = new JSONObject(array.get(i).toString());
            String newStr = jsonObject.getString("acc").replace(jsonObject.getString("acc").substring(0, 2), "XX");
            jsonObject.put("acc", newStr);
            array.put(i, jsonObject);
        }
        System.out.println("Output array : " + array);
    }
}

输出:

Output array : [{"acc":"XX78915","for":"159000"},{"acc":"XX63258","for":"152863"},{"acc":"XX32578","for":"258963"}]


使用java正则表达式

public class Test1 {
    public static void main(String[] args)  {
        String str =  "[{\"acc\":\"1278915\",\"for\":\"159000\"},{\"acc\":\"8963258\",\"for\":\"152863\"},{\"acc\":\"9632578\",\"for\":\"258963\"}]";
        String result = str.replaceAll("(\"acc\":\"[0-9]{2})", "\"acc\":\"xx");
        System.out.println(result);
    }
}

输出:

[{"acc":"xx78915","for":"159000"},{"acc":"xx63258","for":"152863"},{"acc":"xx32578","for":"258963"}]
jaql4c8m

jaql4c8m3#

绳子 textData 表示有效的json格式,因此您可以将其转换为 List<Model> 或者 List<Map<String,String>> 使用objectmapper

public class Model {
  private String acc;
  private String foo; 

  // getters and setters
  }

建模 List<Model> ```
List list = objectMapper.readValue(jsonCarArray, new TypeReference<List>(){});

或“list<map<string,string>>

List<Map<String,String>> list = objectMapper.readValue(jsonCarArray, new TypeReference<List<Map<String,String>>>(){});

然后使用computeifpresent

list.forEach(obj->obj.computeIfPresent("acc",(key,val)-> /*logic to modify value */));

或者另一种迭代方法 `JsonNode` ```
JsonNode response = objectMapper.readTree("responseBody");

    if(response.get("data").isArray()) {
        for (JsonNode node : response.get("data")){
            ObjectNode objectNode = (ObjectNode)node;
            if(objectNode.hasNonNull("acc")){
                String val = objectNode.get("acc").asText();
                objectNode.put("acc","xx"+val.subString(2));
            }
        }
    }
bjp0bcyl

bjp0bcyl4#

response.get("data") 实际上包含另一个json,一个json数组:

[
    {
       "acc":"1278915",
       "for":"159000"
    },
    {
       "acc":"8963258",
       "for":"152863"
    },
    {
       "acc":"9632578",
       "for":"258963"
    }
]

因此,要更改数组中的任何对象,您需要像在任何其他数组中一样迭代这些项:

JsonNode arrayNode = response.get("data");
 if (arrayNode.isArray()) {
     for (JsonNode objectNode : arrayNode) {  // objectNode is again a JSON (object)
         System.out.println(objectNode);   // this would print the object as string like: "{'acc':'912345','for':'123345'}"
         // so to alter that object you get the "acc" node
         // objectNode.get("acc")  -- this is the String "912345", but Strings are immutable so you need to set the new value in the objectNode again
         objectNode.put("acc", "xxxx" + objectNode.get("acc").toString().substring(4));  // it will put the new string value under the key "acc" in the json object
     }
 }

如果没出什么问题 arrayNode 之后应该是这样的:

[
    {
       "acc":"xxxx915",
       "for":"159000"
    },
    {
       "acc":"xxxx258",
       "for":"152863"
    },
    {
       "acc":"xxxx578",
       "for":"258963"
    }
]

相关问题