使用java更新json对象 [英] updating json object using java

查看:334
本文介绍了使用java更新json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我有一个 JSON 对象如下所示

Let i've an JSON Object like as shown below

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

更新

读取json对象,需要更新地址字段,如下所示

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

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

任何人都可以告诉我如何使用<$更新 JSON 中的地址详细信息c $ c> java

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

我的代码

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

更新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 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


推荐答案

解析JSON并调用特定的Key需要更新并设置值钥匙。
请更具体。

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

创建一个新的JSON对象

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

这篇关于使用java更新json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆