有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我们可以逐个读取Json文件负载吗

我们在json文件中有2条记录,我想逐一读取并将其发送到RESTAPI。 我们可以在一个列表中获取这些记录,或者可以在单个有效负载中逐个读取这些记录。我们怎么分呢

  {
    "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"

                }
            }

        ]
    }

共 (1) 个答案

  1. # 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>