发送和解析JSON对象 [英] Sending and Parsing JSON Objects

查看:115
本文介绍了发送和解析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.

我觉得这种格式的从服务器的响应:

I thought of this format for the response from the server:

{
  "post": {
    "username": "someusername",
    "message": "this is a sweet message",
    "image": "http://localhost/someimage.jpg",
    "time":  "present time"
  }
}

如何JSON的多少知识,我应该要达到这个目的?同时这将是巨大的,如果有人可以给我提供了一些教程链接发送和解析JSON对象。

How much knowledge of JSON should I have to accomplish this purpose? Also it would be great if someone could provide me links of some tutorials for sending and parsing JSON Objects.

推荐答案

我很惊讶这些都没有被提及:但不是使用裸机,而手工工艺与json.org的小包装,GSON和杰克逊都方便多了使用。所以:

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,地图,列表,如果你真的想这些,而不是真实的对象)

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)

EDIT 19-MAR-2014:

EDIT 19-MAR-2014:

另一个新的竞争者是杰克逊JR 库:它使用相同的快速流解析器/发生器作为杰克逊(杰克逊核心),但数据绑定的部分很小(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.

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

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