如何转换httpentity json成JSON

httpclient请求方式下的泛型json转换问题
2414次浏览
& 我们在采用httpclient方式进行服务调用时,如果服务方以对象的方式封装入参和出参,我们就经常需要将我们的请求对象先转换成 json 格式,通过输入输出流的方式传输数据,返回的参数再从 json 格式转换为特定对象,如下:
Producer producer = new Producer();
...构造参数...
String jsonData = GsonUtils.toJson(producer);
...httpclient请求...
Consumer consumer = GsonUtils.fromJson(jsonResult, Consumer.class);
& 采用这种方式,可以很容易得到想要的结果,但如果服务方返回的是泛型对象就麻烦了,如下:
GenericResult&Consumer& result = GsonUtils.fromJson(jsonResult,GenericResult.class);
& 这样是可以得到返回的内容,甚至还能在前端取到 result 对象中的基本类型数据,但自定义对象数据就无法取到了。如果使用 Consumer consumer = result.getConsumer(); 就会报错,如下&java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Consumer&之类的类型转换错误,因为泛型转换过程中如果直接使用父类类型从 json 格式转换过来,他是没办法再转换成相应的子类的。更具体的原因是由于Java泛型的实现机制,使用了泛型的代码在运行期间相关的泛型参数的类型会被擦除,我们无法在运行期间获知泛型参数的具体类型(所有的泛型类型在运行时都是Object类型)。
& 解决方法有两种,一种是直接使用resteasy的调用方式,那么根本就不存在这种问题;另一个就是借助 typeToken,只要将需要获取类型的泛型类作为TypeToken的泛型参数构造一个匿名的子类,就可以通过getType()方法获取到我们使用的泛型类的泛型参数类型。代码如下:
Producer producer = new Producer();
...构造参数...
String jsonData = GsonUtils.toJson(producer);
...httpclient请求...
Type type = new TypeToken&GenericResult&Consumer&&(){}.getType();
GenericResult&Consumer& result = (GenericResult&Consumer&)GsonUtils.fromJson(jsonResult, type);
Consumer consumer = result.getConsumer(); & 这时 consumer 对象就能够正确识别了。
您可能也会对以下文章感兴趣
QQ : 341470
Friend Link
New MemberConvertJSON – 转换 Google Reader 导出数据为网页
(快来投票)
Loading...
只需将从 Google Reader 保存在英文目录下,把其中的 json 文件直接拖动到 ConvertJSON 程序图标上,就开始自动转换了,转换后会自动生成一个 html 文件及一个对应文件夹下的一堆 html 文件,点击第一个 html 文件会出现上图的列表形式,逐条点击会出现下面的情况:
是不是应该”保存在非中文目录下”? 话说the old reader的导入功能怎么了,排队人数越来越多了竟然。
按分类查看文章:
大家都在讨论些什么
: journey不缴费不能导出,而国内不能付费: qq已经不再是电脑上最好的截图工具了,1.8.5开始可以选择PrtSc作为热键了。: 他的意思大概是两类问题吧,我也有点疑惑。。。
原文摘录:Now, my friends of this convocation, there is another thing we can hope to learn from your being with us. I illustrate it by quoting the statement of a former college president, and I can understand the reason for his speaking as he did. I am sure President Miller can.
This President said, "I have two kinds of problems, the urgent and the important. The urgent are not important, and the important are never urgent."
Now this, I think, represents a dilemma of modern man. Your being here can help place the important before us, and perhaps even give the important the: 确实是限时免费,不过免费的次数很多就是了。。。: Journey 也很不错啊。: 呃,可能需要代理,只有这么一种解释了: 在哪里注册?填了邮箱&用户名,直接点get started没有反应
最热门标签
传说中的小众软件 让你的手机应用与众不同。
个人 blog 转载时请遵循 “署名-非商业性使用-相同方式共享” 的创作共用协议;
商业网站或未授权媒体不得复制本站内容。[android]Can?t 将 JSON 转换为 JSONObject
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
Here?s 我的 php 文件提供了 JSON:
[{"id":"408","punktezahl":"15","name":"testname","email":"hsksjs","datum":"24.01.14 17:11","wohnort":"Vdhdhs","newsletter":"J"}]
当我尝试访问这样的 JSON 对象
public void connect(){
System.out.println("%%%%%%%%%%%%%%%%%1" );
Thread t = new Thread(){
public void run() {
System.out.println("%%%%%%%%%%%%%%%%%2" );
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);
String urlString = "http://url";
//prepare the HTTP GET call
HttpGet httpget = new HttpGet(urlString);
//get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();
System.out.println("%%%%%%%%%%%%%%%%%3" );
if (entity != null) {
//get the response content as a string
String response = EntityUtils.toString(entity);
//consume the entity
entity.consumeContent();
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
//return the JSON response
JSONObject parentObject = new JSONObject(response);
JSONObject userDetails = parentObject.getJSONObject("output");
String name = userDetails.getString("name");
System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name);
}catch (Exception e) {
e.printStackTrace();
t.start();
出现以下错误:
01-24 18:18:21.746: W/System.err(20673): org.json.JSONException: Value [{"id":"408","datum":"24.01.14 17:11","punktezahl":"15","email":"hsksjs","newsletter":"J","wohnort":"Vdhdhs","name":"testname"}] of type org.json.JSONArray cannot be converted to JSONObject
01-24 18:18:21.746: W/System.err(20673):
at org.json.JSON.typeMismatch(JSON.java:111)
01-24 18:18:21.746: W/System.err(20673):
at org.json.JSONObject.&init&(JSONObject.java:159)
01-24 18:18:21.746: W/System.err(20673):
at org.json.JSONObject.&init&(JSONObject.java:172)
01-24 18:18:21.746: W/System.err(20673):
at com.wuestenfest.jagdenwilli.Highscore_zeigen$1.run(Highscore_zeigen.java:82)
Where?s 我的错吗?
解决方法 1:
你的反应是 JSONArray 不 JSOnObject 。
JSONObject parentObject = new JSONObject(response);
JSONArray jsonarray = new JSONArray(response);
[ // json array node
{ // jsson onject npode
"id": "408",
"punktezahl": "15",
"name": "testname",
"email": "hsksjs",
"datum": "24.01.14 17:11",
"wohnort": "Vdhdhs",
"newsletter": "J"
我看不到任何的 json 对象 output 或者在上述 json。所以
JSONObject userDetails = parentObject.getJSONObject("output");
也是错的。
JSONArray jsonarray = new JSONArray(response);
JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
String name= jb.getString("name");

我要回帖

更多关于 httpentity 转json 的文章

 

随机推荐