java 更新对象,使用java更新json对象

Let i've an JSON Object like as shown below

{"Name":"Manu","Age":"25","Address":""}

Updation

Read the json object and need to update the address field like as given below

{"Name":"Manu","Age":"25","Address":"XXXX"}

can anyone please tell me how to update the Address details in the JSON using java

My code

JSONObject rec = new JSONObject(data);

String name = rec.getString("Name");

String age = rec.getString("Age");

String add = rec.getString("Address");

now how to add some information to the address field

Update 1

String jsonstring="{Name:Manu,Age:25,Address:''}";

JSONObject object=new JSONObject(jsonstring);

JSONObject childobject=object.getJSONObject("Address");

JSONObject modifiedjson=new JSONObject();

modifiedjson.put("type",childobject.get("type"));

modifiedjson.put("value","newvalue");

Exception

Exception in thread "main" org.json.JSONException: JSONObject["Address"] is not a JSONObject.

at org.json.JSONObject.getJSONObject(JSONObject.java:557)

at kotouch.Sample.main(Sample.java:59)

Java Result: 1

解决方案

Parse the JSON and call the particular Key needs to be updated and set the value for the key.

Please be more specific.

Create a new JSON Object

private JSONObject Data = new JSONObject;

public Test(){

try {

Data.put("address", username);

}

catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值