修改Json Object属性,以提供来自同一节点的另一个属性

时间:2019-04-30 08:20:11

标签: java jsonpath rest-assured-jsonpath

我有一个JSON(字符串或文件)。给一个返回列表的节点,我有一个键值元组来标识该列表中的元素并修改另一个属性。

基本上,从节点上,我得到了与典型书籍示例相似的东西

...

[
  {
    "professionalRisk": "Medium",
    "firstName": "Sh�ra",
    "lastName": "Y�ung",
    "riskActivity": "Recreational sports",
    "dateOfBirth": "1991-12-12",
    "@id": "2",
    "title": "Frau",
    "individualTravelPrice": {
      "#text": "20",
      "@currency": "EUR"
    },
    "@role": "INSURED"
  },
  {
    "professionalRisk": "Medium",
    "firstName": "Shashank",
    "lastName": "Tripathi",
    "riskActivity": "Recreational sports",
    "dateOfBirth": "1995-05-23",
    "@id": "3",
    "title": "Herr",
    "individualTravelPrice": {
      "#text": "100",
      "@currency": "EUR"
    },
    "@role": "INSURED"
  },
  {
    "professionalRisk": "Medium",
    "firstName": "J�hn",
    "lastName": "Y�nus",
    "riskActivity": "Recreational sports",
    "dateOfBirth": "2017-12-12",
    "@id": "4",
    "title": "Herr",
    "individualTravelPrice": {
      "#text": "258.15",
      "@currency": "EUR"
    },
    "@role": "INSURED"
  },
  {
    "professionalRisk": "Medium",
    "firstName": "S�m�el",
    "lastName": "Polo",
    "riskActivity": "Recreational sports",
    "dateOfBirth": "1940-05-10",
    "@id": "5",
    "title": "Herr",
    "individualTravelPrice": {
      "#text": "10072",
      "@currency": "EUR"
    },
    "@role": "INSURED"
  },
  {
    "professionalRisk": "Medium",
    "firstName": "Gli �ernch",
    "lastName": "Fedrick",
    "riskActivity": "Recreational sports",
    "dateOfBirth": "1930-12-12",
    "@id": "6",
    "title": "Herr",
    "individualTravelPrice": {
      "#text": "1000",
      "@currency": "EUR"
    },
    "@role": "INSURED"
  }
]

...

我的主要问题是我需要确定其中一个元素并修改其他值。

例如,具有@ Id = 4,我必须将“标题”修改为“ Frau”

我已经导入了以下库:

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder;
import io.restassured.path.json.JsonPath;
import org.apache.commons.beanutils.PropertyUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.Assert;

尝试实施以下不完整的代码

公共字符串setAttribute(String sourceStringOrFilePath,String nodeToSet,String attributeKey,String attributeValue,String attributeToSet,String newValue){     sourceStringOrFilePath = stringOrFilePath(sourceStringOrFilePath,nodeToSet);

ArrayList<String> jsonObject = JsonPath.given(sourceStringOrFilePath).getJsonObject(nodeToSet);


Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonString = gson.toJson(jsonObject);

System.out.println(jsonString);

Iterator i = jsonObject.iterator();

while(i.hasNext()){

    System.out.println(i.next());
}

正确地对此进行迭代

{professionalRisk=Medium, firstName=Sh�ra, lastName=Y�ung, riskActivity=Recreational sports, dateOfBirth=1991-12-12, @id=2, title=Frau, individualTravelPrice={#text=20, @currency=EUR}, @role=INSURED}
{professionalRisk=Medium, firstName=Shashank, lastName=Tripathi, riskActivity=Recreational sports, dateOfBirth=1995-05-23, @id=3, title=Herr, individualTravelPrice={#text=100, @currency=EUR}, @role=INSURED}
{professionalRisk=Medium, firstName=J�hn, lastName=Y�nus, riskActivity=Recreational sports, dateOfBirth=2017-12-12, **@id=4, title=Herr**, individualTravelPrice={#text=258.15, @currency=EUR}, @role=INSURED}
{professionalRisk=Medium, firstName=S�m�el, lastName=Polo, riskActivity=Recreational sports, dateOfBirth=1940-05-10, @id=5, title=Herr, individualTravelPrice={#text=10072, @currency=EUR}, @role=INSURED}
{professionalRisk=Medium, firstName=Gli �ernch, lastName=Fedrick, riskActivity=Recreational sports, dateOfBirth=1930-12-12, @id=6, title=Herr, individualTravelPrice={#text=1000, @currency=EUR}, @role=INSURED}

需要标识和修改**中的属性

解决了这个问题之后,我已经有了一个功能,可以在指示的原始路径中设置修改后的节点

0 个答案:

没有答案