JSONObject获取的值有时候不是String类型,而有时候又是String类型,怎么办呐

{
"weatherinfo": {
"city": "深圳",
"cityid": "101280601",
"temp1": "24℃",
"temp2": "30℃",
"weather": "阵雨转大雨",
"img1": "n3.gif",
"img2": "d9.gif",
"ptime": "18:00"
}
}

 

package api;


import com.oracle.javafx.jmx.json.JSONException;
import org.json.JSONObject;

public class getValue {

    /**
     * 解析Json内容
     * @return JsonValue 返回JsonString中JsonId对应的Value
     **/

    public static String getJsonValue(String JsonString, String JsonId) {
        String JsonValue ="";
        if (JsonString == null || JsonString.trim().length() < 1) {
            System.out.println("please inout JsonString");
            return null;
        }
        try {
            JSONObject obj = new JSONObject(JsonString);
            JsonValue = obj.getString(JsonId);
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return JsonValue;
    }

}

  

import api.getValue;
import api.getResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestMsParis {


    public String httpResult= null;
    public static String mobile="";



    @Test
    public void getHttp() throws IOException {




        List<NameValuePair> param =new ArrayList<NameValuePair>();
        NameValuePair nameValuePair = new BasicNameValuePair("platform", "wap");
        param.add(nameValuePair);

        NameValuePair nameValuePair1 = new BasicNameValuePair("mobile","18962873440");
        param.add(nameValuePair1);


        getResponse get = new getResponse();
        get.request("GET", "http://api.v2.msparis.com/common/sms", param);



        }


    @Test
    public void getServer() throws IOException {

        String httpResult= null, city=null,exp_city = null;
        String cityCode="";

        List<NameValuePair> param = new ArrayList<NameValuePair>();
        NameValuePair nameValuePair = new BasicNameValuePair("platform", "wap");
        param.add(nameValuePair);

        NameValuePair nameValuePair1 = new BasicNameValuePair("mobile", "18962873440");
        param.add(nameValuePair1);
        getResponse weather = new getResponse();
        httpResult =weather.request("GET", "http://www.weather.com.cn/data/cityinfo/101280601.html", param);

        exp_city="深圳";
        cityCode="101280601";
        System.out.println("请求地址: "+weather.getClass());
        System.out.println("返回结果: "+httpResult);
        String weatherinfo=getValue.getJsonValue(httpResult, "weatherinfo");
        System.out.println("weatherinfo: "+weatherinfo);
        city=getValue.getJsonValue(weatherinfo,"city");
        System.out.println("city: "+weatherinfo);
        System.out.println("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
        Assert.assertEquals(city,exp_city);

    }


    @Test
    public void getHttps() throws IOException {

            getResponse get3 = new getResponse();
            List<NameValuePair> param3 =new ArrayList<NameValuePair>();
            NameValuePair nameValuePair3 = new BasicNameValuePair("phone", "18962873440");
            param3.add(nameValuePair3);
            get3.request("GET", "https://pintuan.msparis.com/Api/God/getCode", param3);
        }

}

  

 

 

在testNG做校验的时候,报错:

org.json.JSONException: JSONObject["weatherinfo"] not a string.

at org.json.JSONObject.getString(JSONObject.java:810)
at api.getValue.getJsonValue(getValue.java:22)
at TestMsParis.getServer(TestMsParis.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

 

 



转载于:https://www.cnblogs.com/qianjinyan/p/9471973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值