淺談JSONObject解析JSON數據

咱們在作jmeter接口測試時能會用beanshell斷言,通常都會將返回值轉成JSONObject對象進行處理。本文選取較爲複雜json格式數據,也將適用於java接口測試。java

JSON數據

{
        "data": {
            "city": "深圳",
            "date": "2017-11-04",
            "pm25": {
                "aqi": 0,
                "co": 8,
                "updatetime": "2017-11-04 13:00:00"
            },
            "daily": [
                {
                    "date": "2017-11-04",
                    "week": "星期六",
                    "templow": "19",
                    "weather": "多雲"
                },
                {
                    "date": "2017-11-05",
                    "week": "星期日",
                    "sunrise": "06:29",
                    "weather": "多雲"
                }
            ]
        },
        "status": 0,
        "msg": "ok"
    }

解析JSON

如下代碼使用HttpClient進行接口測試,同時使用testNg進行斷言shell

package apitest.cases;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;


import java.net.URI;

public class Weather {
    @Test
    public void weather() {
        HttpClient httpClient = HttpClientBuilder.create().build();


        //執行post方法
        try {
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(URI.create("http://localhost:8001/weather"));

            //聲明一個對象來進行響應結果的存儲
            HttpResponse response = httpClient.execute(httpGet);
            //獲取響應結果將格式轉化爲Json數據
            String response2 = EntityUtils.toString(response.getEntity(), "utf-8");
            JSONObject responseJson = new JSONObject(response2);

            // 獲取msg status
            String msg = responseJson.getString("msg");
            String status = responseJson.get("status").toString();


            // 獲取data
            JSONObject jsonData = responseJson.getJSONObject("data");

            // 獲取data裏的city
            String city = jsonData.getString("city");

            // 獲取pm25
            JSONObject pm25 = jsonData.getJSONObject("pm25");

            // 獲取pm25裏的updatetime
            String updateTime = pm25.getString("updatetime");

            // 獲取data裏的daily
            JSONArray jsonDaily = jsonData.getJSONArray("daily");

            // 獲取data裏的daily的第一組數據
            JSONObject jsonDailyFirst = jsonDaily.getJSONObject(0);

            // 獲取data裏的daily的第一組數據的date
            String date = jsonDailyFirst.getString("date");


            System.out.println("獲取msg----" + msg);
            System.out.println("獲取status----" + status);
            System.out.println("獲取data----" + jsonData);
            System.out.println("獲取data裏的city----" + city);
            System.out.println("獲取pm25----" + pm25);
            System.out.println("獲取pm25裏的updateTime----" + updateTime);
            System.out.println("獲取data裏的daily----" + jsonDaily);
            System.out.println("獲取data裏的daily的第一組數據----" + jsonDailyFirst);
            System.out.println("獲取data裏的daily的第一組數據的date----" + date);


            //斷言
            Assert.assertEquals(msg, "ok");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

軟件測試汪簡書地址
軟件測試汪博客地址apache

歡迎關注微信公衆號:軟件測試汪。軟件測試交流羣:809111560json

轉載請注意出處,謝謝合做api

相關文章
相關標籤/搜索