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

												

JSONObject获取的值有时候不是String类型,而有时候又是String类型,怎么办呐的更多相关文章

  1. PropertiesUtil 获取文件属性值

    有时候不要把一些属性值写死在代码中,而是写在配置在文件中,方便更改 PropertiesUtil工具类:读取key-value形式的配置文件,根据key获得value值  1.测试类 public c ...

  2. 模拟HTTP请求获取返回值

    package org.jeecgframework.core.util; import java.io.BufferedReader; import java.io.IOException; imp ...

  3. 根据数据中的key获取value值

    一.测试数据准备 List<Map<String, String>> result = new ArrayList();Map<String, String> ma ...

  4. Java--FutureTask原理与使用(FutureTask可以被Thread执行,可以被线程池submit方法执行,并且可以监控线程与获取返回值)

    package com; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; i ...

  5. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  6. 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值

    接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...

  7. storm源码之巧用java反射反序列化clojure的defrecord获取属性值

    [原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 [原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 storm源 ...

  8. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  9. ResDrawableImgUtil【根据图片名称获取resID值或者Bitmap对象】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据图片名称获取项目的res/drawable-xxdhpi中相应资源的ID值以及bitmap值的封装类. 效果图 代码分析 根据图 ...

随机推荐

  1. Andrew Ng-ML-第八章-正则化

    1.过度拟合overfitting 过度拟合,因为有太多的特征+过少的训练数据,学习到的假设可能很适应训练集,但是不能泛化到新的样例.即泛化generalize能力差. 解决办法: 1.手动/使用选择 ...

  2. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. HTTP请求返回状态码详解

    当用户试图通过 HTTP 访问一台正在运行 Internet 信息服务 (IIS) 的服务器上的内容时,IIS 返回一个表示该请求的状态的数字代码.状态代码可以指明具体请求是否已成功,还可以揭示请求失 ...

  4. Promise学习探究

    学习熟知吧,原理还是继续吧 例子1: var isGeted; function getRet(){ return new Promise(function(resolve, reject) { // ...

  5. 概率检索模型:BIM+BM25+BM25F

    1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...

  6. 谷歌浏览器 URL无法访问

    使用谷歌浏览器老是会崩溃,或者访问的时候发现“URL无法访问”等失败的问题,连淘宝都没法访问,这个让人很恼火, 最后在扩展应用那里搜到个URL的redirect,问题解决了,~~发现没有再出现类似问题 ...

  7. Python:re.sub()实现字符串替换

    re.sub的功能 re是regular expression的缩写,表示正则表达式:sub是substitude的缩写,表示替换 re.sub是正则表达式的函数,实现比普通字符串更强大的替换功能 s ...

  8. Linux基础命令---swapoff

    swapoff 关闭交换分区,同时可以刷新交换分区的缓存.关闭交换分区后,使用free指令查看内存,swap数值会减少.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE. ...

  9. DeepMind已将AlphaGo引入多领域 Al泡沫严重

    DeepMind已将AlphaGo引入多领域 Al泡沫严重 在稳操胜券的前提下,谷歌旗下的AlphaGo还是向柯洁下了战书.4月10日,由中国围棋协会.浙江省体育局.谷歌三方联合宣布,将于5月23日至 ...

  10. linux chkconfig 管理服务开机自启动

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...