JSON 还是 SOAP (XML)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1237649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON or SOAP (XML)?
提问by Fulkron
I'm developing a new application for the company. The application have to exchange data from and to iPhone.
我正在为公司开发一个新的应用程序。该应用程序必须与 iPhone 交换数据。
Company server side uses .NET framework.
公司服务器端使用.NET框架。
For example: the class "Customer" (Name, Address etc..) for a specific CustomerNumber should be first downloaded from server to iphone, stored locally and then uploaded back to apply changes (and make them available to other people). Concurrency should not be a problem (at least at this time...)
例如:特定 CustomerNumber 的类“Customer”(姓名、地址等)应该首先从服务器下载到 iphone,存储在本地,然后上传回应用更改(并使它们可供其他人使用)。并发应该不是问题(至少在这个时候......)
In any case I have to develop both the server side (webservice or whatever) and the iPhone app.
无论如何,我必须同时开发服务器端(网络服务或其他)和 iPhone 应用程序。
I'm free to identify the best way to do that (this is the application "number ONE" so it will become the "standard" for the future).
我可以自由确定最好的方法(这是应用程序“第一”,因此它将成为未来的“标准”)。
So, what do you suggest me ?
那么,你对我有什么建议?
Use SOAP web services (XML parsing etc..) or user JSON ? (it seems lighter...) Is it clear to me how to "upload" data using SOAP (very long to code the xml soap envelope ... I would avoid) but how can I do the same using JSON ?
使用 SOAP Web 服务(XML 解析等)或用户 JSON ?(它似乎更轻......)我是否清楚如何使用SOAP“上传”数据(编码xmlsoap信封很长......我会避免)但是我如何使用JSON做同样的事情?
The application needs to use date values (for example: last_visit_date etc..) what about date in Json ?
应用程序需要使用日期值(例如:last_visit_date 等),那么 Json 中的日期呢?
采纳答案by gbjbaanb
JSON has several advantages over XML. Its a lot smaller and less bloated, so you will be passing much less data over the network - which in the case of a mobile device will make a considerable difference.
与 XML 相比,JSON 有几个优点。它更小,更不臃肿,因此您将通过网络传递更少的数据 - 这在移动设备的情况下会产生相当大的差异。
Its also easier to use in javascript code as you can simply pass the data packet directly into a javascript array without any parsing, extracting and converting, so it is much less CPU intensive too.
它也更容易在 javascript 代码中使用,因为您可以简单地将数据包直接传递到 javascript 数组中,而无需任何解析、提取和转换,因此 CPU 密集度也低得多。
To code with it, instead of an XML library, you will want a JSON library. Dates are handled as you would with XML - encode them to a standard, then let the library recognise them. (eg here's a librarywith a sample with dates in it)
要使用它进行编码,您需要一个 JSON 库,而不是 XML 库。日期的处理方式与 XML 相同 - 将它们编码为标准,然后让库识别它们。(例如,这是一个带有日期样本的图书馆)
回答by Avi
Ah, the big question: JSON or XML?
啊,最大的问题是:JSON 还是 XML?
In general, I would prefer XML only when I need to pass around a lot of text, since XML excels at wrapping and marking up text.
一般来说,只有当我需要传递大量文本时,我才更喜欢 XML,因为 XML 擅长包装和标记文本。
When passing around small data objects, where the only strings are small (ids, dates, etc.), I would tend to use JSON, as it is smaller, easier to parse, and more readable.
当传递小数据对象时,其中唯一的字符串很小(ID、日期等),我倾向于使用 JSON,因为它更小、更容易解析且更具可读性。
Also, note that even if you choose XML, that does not by any means mean you need to use SOAP. SOAP is a very heavy-weight protocol, designed for interoperability between partners. As you control both the client and server here, it doesn't necessarily make sense.
另外,请注意,即使您选择 XML,也绝不意味着您需要使用 SOAP。SOAP 是一个非常重要的协议,旨在实现合作伙伴之间的互操作性。当您在这里控制客户端和服务器时,这不一定有意义。
回答by JBRWilkinson
Consider how you'd be consuming the results on the iPhone. What mechansim would you use to read the web service response? NSXMLParser?
考虑一下您将如何在 iPhone 上使用结果。您将使用什么机制来读取 Web 服务响应?NSXMLParser?
How you consume the data would have the biggest impact on how your serve it.
您使用数据的方式将对您的服务方式产生最大的影响。
Are JSON and SOAP your only options? What about RESTful services?
JSON 和 SOAP 是您唯一的选择吗?RESTful 服务呢?
Take a look at some big players on the web that have public APIs that are accessible by iPhone clients:
看看网络上的一些大公司,它们拥有可供 iPhone 客户端访问的公共 API:
Also, review the following related articles:
另外,请查看以下相关文章:
回答by back2dos
JSON has following advantages:
JSON 具有以下优点:
- it can encode boolean and numeric values ... in XML everything is a string
- it has much clearer semantics ... in json you have
{"key":"someValue"}, in XML you can have<data><key>someValue</key></data>or<data key="someValue" />... any XML node must have a name ... this does not always make sense ... and children may either represent properties of an object, or children, which when occuring multiple times actually represent an array ... to really understand the object structure of an XML message, you need its corresponding schema ... in JSON, you need the JSON only ... - smaller and thus uses less bandwidth and memory during parsing/generation ...
- 它可以编码布尔值和数字值……在 XML 中,一切都是字符串
- 它有更清晰的语义......在 json 中你有
{"key":"someValue"},在 XML 中你可以有<data><key>someValue</key></data>或者<data key="someValue" />......任何 XML 节点都必须有一个名称......这并不总是有意义......并且孩子们可能代表一个对象的属性,或 children,当多次出现时实际上代表一个数组……要真正理解 XML 消息的对象结构,您需要其相应的架构……在 JSON 中,您只需要 JSON…… - 更小,因此在解析/生成过程中使用更少的带宽和内存......
apart from that, i see NO difference between XML and JSON ... i mean, this is so interchangable ... you can use JSON to capture the semantics of SOAP, if you want to ... it's just that SOAP is so bloated ... if you do want to use SOAP, use a library and generators for that ... it's neither fun nor interesting to build it all by hand ...
除此之外,我认为 XML 和 JSON 之间没有区别……我的意思是,这是可以互换的……如果您愿意,可以使用 JSON 来捕获 SOAP 的语义……只是 SOAP 太臃肿了... 如果您确实想使用 SOAP,请为此使用一个库和生成器 ... 手工构建它既不有趣也不有趣 ...
using XML RPC or JSON RPC should work faster ... it is more lightweight, and you use JSON or XML at will ... but when creating client<->server apps, a very important thing in my eyes, is to abstract the transport layer on both sides ... your whole business logic etc. should in no way depend on more than a tiny interface, when it comes to communication, and then you can plug in protocols into your app, as needed ...
使用 XML RPC 或 JSON RPC 应该工作得更快......它更轻量级,你可以随意使用 JSON 或 XML......但是在创建客户端<->服务器应用程序时,在我看来非常重要的事情是抽象双方的传输层......你的整个业务逻辑等绝不应该依赖于一个微小的接口,在通信方面,然后你可以根据需要将协议插入你的应用程序......
回答by Norman
There are more options than just SOAP vs JSON. You can do a REST-based protocol (Representational State Transfer) using XML. I think it's easier use than SOAP and you get a much nicer XSD (that you design.) It's rather easy for almost any client to access such services.
除了 SOAP 与 JSON 之外,还有更多选择。您可以使用 XML执行基于 REST 的协议(Representational State Transfer)。我认为它比 SOAP 更容易使用,并且您可以获得更好的 XSD(您设计的)。几乎所有客户端都可以轻松访问此类服务。
On the other hand, JSON parsers are available for almost any language and make it really easy to call from JavaScript if you'll use them via AJAX.
另一方面,JSON 解析器几乎适用于任何语言,如果您通过 AJAX 使用它们,那么从 JavaScript 调用它们就变得非常容易。
However, SOAP can be rather powerful with tons of standardized extensions that support enterprise features.
但是,SOAP 可以通过大量支持企业功能的标准化扩展而变得相当强大。
回答by PeyloW
You could also use Hessian using HessianKit on the iPhone side, and HessianC# on the server side.
您还可以在 iPhone 端使用 HessianKit 使用 Hessian,在服务器端使用 HessianC#。
The big bonuses are: 1. Hessian in a binary serialization protocol, so smaller data payloads, good for 3G and GSM. 2. You do not need to worry about format in either end, transport is automated with proxies.
最大的好处是: 1. 二进制序列化协议中的 Hessian,因此数据负载更小,适用于 3G 和 GSM。2.您无需担心两端的格式,传输是通过代理自动进行的。
So on the server side you just define an C# interface, such as:
所以在服务器端你只需定义一个 C# 接口,例如:
public interface IFruitService {
int FruitCount();
string GetFruit(int index);
}
Then you just subclass CHessianHandler and implement the IFruitService, and your web service is done.
然后您只需继承 CHessianHandler 并实现 IFruitService,您的 Web 服务就完成了。
On the iPhone just write the corresponding Objective-C protocol:
在 iPhone 上只需编写相应的 Objective-C 协议:
@protocol IFruitService
-(int)FruitCount;
-(NSString*)GetFruit:(int)index;
@end
That can then be access by proxy by a single line of code:
然后可以通过一行代码通过代理访问:
id<IFruitService> fruitService = [CWHessianConnection proxyWithURL:serviceURL
protocol:@protocol(IFruitService)];
Links:
链接:
HessianKit : hessiankit
HessianKit : hessiankit
回答by Marin Todorov
I would certainly go with JSON, as others already noted - it's faster and data size is smaller. You can also use a data modelling framework like JSONModel to validate the JSON structure, and to autoconvert JSON objects to Obj-C objects.
正如其他人已经指出的那样,我肯定会使用 JSON - 它更快,数据量更小。您还可以使用像 JSONModel 这样的数据建模框架来验证 JSON 结构,并将 JSON 对象自动转换为 Obj-C 对象。
JSONModel also includes classes for networking and working with APIs - also includes json rpc methods.
JSONModel 还包括用于联网和使用 API 的类 - 还包括 json rpc 方法。
Have a look at these links:
看看这些链接:
- http://www.jsonmodel.com- the JSONModel framework
- http://json-rpc.org- specification for JSON APIs implementation
- http://www.charlesproxy.com- the best tool to debug JSON APIs
- http://json-schema.org- tool to define validation schemas for JSON, useful along the way
- http://www.jsonmodel.com- JSONModel 框架
- http://json-rpc.org- JSON API 实现规范
- http://www.charlesproxy.com- 调试 JSON API 的最佳工具
- http://json-schema.org- 为 JSON 定义验证模式的工具,一路有用
Short example of using JSONModel: http://www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/
使用 JSONModel 的简短示例:http: //www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/
Hope these are useful
希望这些有用

