我们可以逐个读取Json文件的有效负载吗?

时间:2018-03-24 16:00:34

标签: java json gson testng dataprovider

我们在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"

                }
            }

        ]
    }

1 个答案:

答案 0 :(得分:0)

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

createClassFromWsdl('https://www.zarinpal.com/pg/services/WebGate/wsdl');

obj = PaymentGatewayImplementationService;

PaymentRequest(obj,'9b9eef82-7cde-11e7-b794-000c295eb8fc', 100 ,'comment','','','www.google.com')

这是一个示例代码,演示了如何将此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"
        }
      }
    ]
  }
]

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

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;
    }
}