使用gson将新属性添加到特定的json节点 [英] Add a new property to a specific json node using gson

查看:1078
本文介绍了使用gson将新属性添加到特定的json节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索gson,想检查我是否可以删除和添加元素。我有下面的json

  {
header:{
timeStamp:2016- 02-09T15:22:36.107-08:00,
uniqueid:321ef660,
},
body:{
search:{
searchId:9206422282,
DateFrom:2016-04-15T00:00:00-07:00,
DateTo:2016-06-24T00:00: 00-07:00

},
金额:[
{
金额:73.704285,
currency:美元

amountagain:{
金额:96.791435,
货币:美元
},
获奖者:null,
pgoodId:null,
},

和现在我想在body下添加一个新元素,如:

  {
header:{
timeStamp:2016-02-09T15:22:36.107-08:00,
uniqueid:321c5690-1d2e-4403-9c31-029cc47ef660,
},
body:{
search:{
searchId:9206422282,
DateFrom:2016-04-15T00:00:00-07:00,
DateTo:2016-06-24T00:00:00-07:00
AddANewFieldHere:**2016-04-18**
}
}

当我做时

  JsonObject jsonObject = new JsonObject(); 
try {
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(新的FileReader(src / main / resources / search.json));
jsonObject = jsonElement.getAsJsonObject();
} catch(FileNotFoundException e){

} catch(IOException ioe){

}

// jsonObject.get(报到);
jsonObject.addProperty(AddANewFieldHere,2016-04-18);
System.out.print(jsonObject);




$ p 它在文档末尾添加了这个属性,而不是我期望在body下。

解决方案

jsonObject 是根节点。您需要导航到您要修改的节点。

  JsonObject body = jsonObject.getAsJsonObject(body); 
body.addProperty(AddANewFieldHere,2016-04-18);

在示例输出中,它看起来像希望它位于路径 body / search / searchId not body 虽然:

  JsonObject searchId = jsonObject 
.getAsJsonObject(body)
.getAsJsonObject(search)
.getAsJsonObject(searchId);
searchId.addProperty(AddANewFieldHere,2016-04-18);


I am exploring gson and wanted to check if I can remove and add elements. I have the below json

{
  "header": {
"timeStamp": "2016-02-09T15:22:36.107-08:00",
"uniqueid": "321ef660",
},
"body": {
"search": {
  "searchId": 9206422282,
      "DateFrom": "2016-04-15T00:00:00-07:00",
      "DateTo": "2016-06-24T00:00:00-07:00"
    }
  },
  "amount": [
    {   
        "amount": 73.704285,
        "currency": "USD"
      },
      "amountagain": {
        "amount": 96.791435,
        "currency": "USD"
      },
      "winners": null,
      "pgoodId": null,
      },

and now I want to add a new element under body like :

{
"header": {
"timeStamp": "2016-02-09T15:22:36.107-08:00",
"uniqueid": "321c5690-1d2e-4403-9c31-029cc47ef660",
},
"body": {
"search": {
  "searchId": 9206422282,
      "DateFrom": "2016-04-15T00:00:00-07:00",
      "DateTo": "2016-06-24T00:00:00-07:00"
      "AddANewFieldHere" : **"2016-04-18"**
    }
  }

when I do

JsonObject jsonObject = new JsonObject();
    try {
        JsonParser parser = new JsonParser();
        JsonElement jsonElement = parser.parse(new FileReader("src/main/resources/search.json"));
        jsonObject = jsonElement.getAsJsonObject();
    } catch (FileNotFoundException e) {

    } catch (IOException ioe){

    }

//    jsonObject.get("checkin");
jsonObject.addProperty("AddANewFieldHere","2016-04-18");
    System.out.print(jsonObject);

}

It adds this property at the end of the document not as I expect under body.

解决方案

jsonObject is the root node. You need to navigate to the node you want to modify.

JsonObject body = jsonObject.getAsJsonObject("body");
body.addProperty("AddANewFieldHere","2016-04-18");

From the example output, it looks like want it under the path body/search/searchId not body though:

JsonObject searchId = jsonObject
                .getAsJsonObject("body")
                .getAsJsonObject("search")
                .getAsJsonObject("searchId");
searchId.addProperty("AddANewFieldHere","2016-04-18");

这篇关于使用gson将新属性添加到特定的json节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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