是否可以使用JDK或HttpComponents处理JSON响应

是否可以使用JDK或HttpComponents处理JSON响应

本文介绍了是否可以使用JDK或HttpComponents处理JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在升级我们的网络应用程序以使用Facebook的Graph API,该API返回JSON响应。但是,除非我们没有其他选择,否则我们不希望添加依赖于JSON库。对于服务器端http请求,我们使用Apache HttpComponents。

we are upgrading our web app to use Facebook's Graph API, which returns JSON responses. However we don't want to add dependecy to a JSON library unless we have no other choice. For server-side http requests we use Apache HttpComponents.

因此,我的问题是JDK和/或HttpComponents中的类(如果有)可以用于处理JSON响应?代码片段欢迎:)

Thus, my question is what are the classes (if any) in the JDK and/or in HttpComponents that I can use to process JSON responses? Code snippets are welcome :)

推荐答案

有是 @McDowell提到的API,但是很简单。您可以自己编写一个基于堆栈的解析器,它确定JSON字符串char-by-char并将JSON数组 [] 转换为 Object [] 列表< Object> 和JSON对象 {} String,Object> 。这是一个很好的学习练习。但是,当您不想担心长期的稳健性和维护问题时,建议您选择一个位于。我个人最喜欢的是。它可以很好地将复杂的JSON结构转换为完整可重用的Javabeans。

There is the javax.script API as mentioned by @McDowell, but the JSON syntax is pretty trivial. You could write a stackbased parser yourself which determines the JSON string char-by-char and converts JSON arrays [] to a Object[] or List<Object> and JSON objects {} to a Map<String, Object>. It's a nice learning exercise. However, when you don't want to worry about long term robustness and maintenance, then I'd suggest to just pick one of the Java libraries listed at the bottom of this page. My personal favourite is Google Gson. It can nicely convert complex JSON structures into fullworthy and reuseable Javabeans.

这篇关于是否可以使用JDK或HttpComponents处理JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!