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

查看:32
本文介绍了在 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?

推荐答案

我很惊讶这些没有被提及:但是,GSon 和 Jackson 使用 json.org 的小包,而不是使用准系统而不是手动流程,要方便得多使用.所以:

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:

因此您实际上可以绑定到您自己的 POJO,而不是一些半途而废的树节点或列表和映射.(至少 Jackson 也允许绑定到这些东西(也许 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 Beans),但性能方面应该很快,并且初始化(第一次调用)开销也非常低.所以它可能是不错的选择,尤其是对于较小的应用程序.

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天全站免登陆