在Android中发送和解析JSON对象 [英] Sending and Parsing JSON Objects in Android

查看:81
本文介绍了在Android中发送和解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以JSON对象的形式将消息发送到服务器,并解析来自服务器的JSON响应.

I would like to send messages in the form of JSON objects to a server and parse the JSON response from the server.

JSON对象示例

{
  "post": {
    "username": "John Doe",
    "message": "test message",
    "image": "image url",
    "time":  "current time"
  }
}

我试图通过逐个属性手动解析JSON.我可以使用任何库/实用工具来简化此过程吗?

I am trying to parse the JSON manually by going attribute by attribute. Is there any library/utility I can use to make this process easier?

推荐答案

我很惊讶没有提到这些:但是,与使用json.org的小程序包进行简单的手工操作相比,GSon和Jackson更方便了使用.所以:

I am surprised these have not been mentioned: but instead of using bare-bones rather manual process with json.org's little package, GSon and Jackson are much more convenient to use. So:

  • GSON
  • Jackson

因此,您实际上可以绑定到自己的POJO,而不是绑定到某些半确定的树节点或列表和地图. (至少杰克逊也允许绑定到这些东西(也许还有GSON,也不确定),如果您真的想要这些而不是真实的"对象,则可以使用JsonNode,Map,List)

So you can actually bind to your own POJOs, not some half-assed tree nodes or Lists and Maps. (and at least Jackson allows binding to such things too (perhaps GSON as well, not sure), JsonNode, Map, List, if you really want these instead of 'real' objects)

编辑2014年3月19日:

EDIT 19-MAR-2014:

另一个新的竞争者是 Jackson jr 库:它使用与Jackson相同的快速流解析器/生成器(jackson-core),但数据绑定部分很小(50kB).功能受到更多限制(没有注释,只有常规的Java Bean),但是性能方面应该很快,并且初始化(首次调用)开销也非常低. 因此,这也许是个不错的选择,尤其是对于较小的应用程序.

Another new contender is Jackson jr library: it uses same fast Streaming parser/generator as Jackson (jackson-core), but data-binding part is tiny (50kB). Functionality is more limited (no annotations, just regular Java Beans), but performance-wise should be fast, and initialization (first-call) overhead very low as well. So it just might be good choice, especially for smaller apps.

这篇关于在Android中发送和解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆