我们可以一一读取 Json 文件负载吗
Can we read Json file payload one by one
我们在json文件中有2条记录,我想一一读取并发送给休息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"
}
}
]
}
假设您的 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>
我们在json文件中有2条记录,我想一一读取并发送给休息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"
}
}
]
}
假设您的 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>