【问题标题】:Can we read Json file payload one by one能不能一一读取Json文件payload
【发布时间】:2018-03-24 16:00:33
【问题描述】:

我们在 json 文件中有 2 条记录,我想一一读取并发送到 rest api。 我们可以在列表中获取这些记录,还是可以在单个有效负载中逐一读取此记录。怎么拆分?

  {
    "batchSize": 0,
    "debug": true,
    "records": [
        {
            "firstName": "Maria Farj Hassan",
            "address": {
                "postcode": "100001",
                "longitude": 180
            },
            "birthDay": "1980-06-30",
            "startTime": 1485167477,
            "_meta": {
                "type": "customer.dedup",
                "id": "1",
                "status": "ACTIVE",
                "businessId": "1",

            }
        }

    ]
}

第二条记录是::

 {
        "batchSize": 0,
        "debug": true,
        "records": [
            {
                "firstName": "Maria  Hassan",
                "address": {
                    "postcode": "100001",
                    "longitude": 180
                },
                "birthDay": "1980-06-30",
                "partyType": 1,
                "startTime": 1485167477,
                "_meta": {
                    "type": "customer.dedup",
                    "id": "1",
                    "status": "ACTIVE"

                }
            }

        ]
    }

【问题讨论】:

    标签: java json gson testng dataprovider


    【解决方案1】:

    假设您的 Json 文件如下所示(我只是将您的两条记录合并到一个 JSON 数组中)

    [
      {
        "batchSize": 0,
        "debug": true,
        "records": [
          {
            "firstName": "Maria  Hassan",
            "address": {
              "postcode": "100001",
              "longitude": 180
            },
            "birthDay": "1980-06-30",
            "partyType": 1,
            "startTime": 1485167477,
            "_meta": {
              "type": "customer.dedup",
              "id": "1",
              "status": "ACTIVE"
            }
          }
        ]
      },
      {
        "batchSize": 0,
        "debug": true,
        "records": [
          {
            "firstName": "Maria Farj Hassan",
            "address": {
              "postcode": "100001",
              "longitude": 180
            },
            "birthDay": "1980-06-30",
            "startTime": 1485167477,
            "_meta": {
              "type": "customer.dedup",
              "id": "1",
              "status": "ACTIVE",
              "businessId": "1"
            }
          }
        ]
      }
    ]
    

    这是一个示例代码,展示了如何将此 json 文件与数据提供程序一起使用。

    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    
    public class TestSample {
        @Test(dataProvider = "dp")
        public void testMethod(JsonElement record) {
            System.err.println("Data : " + record.getAsJsonObject().toString());
        }
    
        @DataProvider(name = "dp")
        public Object[][] getData() throws FileNotFoundException {
            JsonArray array = new JsonParser().parse(new FileReader("src/test/resources/49466822.json")).getAsJsonArray();
            Object[][] data = new Object[array.size()][1];
            for (int i = 0; i < array.size(); i++) {
                data[i][0] = array.get(i);
            }
            return data;
        }
    }
    

    您需要将以下内容添加为 Maven 依赖项(Google gson 依赖项)

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多