java中 {"town":"园洲镇","city":"惠州市","county":"博罗县","mobile":"135*******6","telephone":"135*******6","cityId":"1643","townId":"36223","provinceId":"19","province":"广东","countyId":"36178","fullAddress":"AAT0P3HQPvKlCnegRFUk5bdiTJbIaVk36X/dm1Lzow6CvV38aGkQP1IXB+qzFkBLdLlFw+TjBoQXP5mLqjo0liarZremN5TPr4sUvuFvPnRV+30+TSuq5+1njoKXxE+aSxU=","fullname":"AAT0P3HQPvKlCnegRFUk5bdigtJlCHYkgy61jPcmo4ckvZ2QT+BSRb+cAq675jWKaSs=","desen_telephone":"666231fb1639e30370794864bd6e91f23feb661e25ce7a235d5647a76512e64c9b20c3","desen_mobile":"666231fb1639e30370794864bd6e91f23feb661e25ce7a235d5647a76512e64c9b20c3"} 我想把这个json某个key改一个值 ,怎么改方便
时间: 2023-06-28 11:04:57 浏览: 175
你可以使用Java中的JSON库来解析和修改JSON字符串。以下是一个示例代码:
```java
import org.json.JSONObject;
public class ModifyJson {
public static void main(String[] args) {
String jsonStr = "{\"town\":\"园洲镇\",\"city\":\"惠州市\",\"county\":\"博罗县\",\"mobile\":\"135*******6\",\"telephone\":\"135*******6\",\"cityId\":\"1643\",\"townId\":\"36223\",\"provinceId\":\"19\",\"province\":\"广东\",\"countyId\":\"36178\",\"fullAddress\":\"AAT0P3HQPvKlCnegRFUk5bdiTJbIaVk36X/dm1Lzow6CvV38aGkQP1IXB+qzFkBLdLlFw+TjBoQXP5mLqjo0liarZremN5TPr4sUvuFvPnRV+30+TSuq5+1njoKXxE+aSxU=\",\"fullname\":\"AAT0P3HQPvKlCnegRFUk5bdigtJlCHYkgy61jPcmo4ckvZ2QT+BSRb+cAq675jWKaSs=\",\"desen_telephone\":\"666231fb1639e30370794864bd6e91f23feb661e25ce7a235d5647a76512e64c9b20c3\",\"desen_mobile\":\"666231fb1639e30370794864bd6e91f23feb661e25ce7a235d5647a76512e64c9b20c3\"}";
JSONObject jsonObj = new JSONObject(jsonStr);
// 修改mobile的值
jsonObj.put("mobile", "137*******8");
// 打印修改后的JSON字符串
System.out.println(jsonObj.toString());
}
}
```
在这个示例代码中,我们首先使用JSONObject类将JSON字符串转换为Java对象。然后,我们使用put方法修改mobile键的值。最后,我们使用toString方法将修改后的JSONObject对象转换回JSON字符串,并打印输出。
阅读全文