简体   繁体   English

如何解析可以是文本,JSON或XML的响应

[英]How to parse a response which can be text or JSON or XML

I am trying to connect to a website. 我正在尝试连接到一个网站。 After While authentication is successful, I get the following response: 在身份验证成功之后,我得到以下响应:

{"code": "OK", 
 "data": {"session_id": "apimanager@taurusseo.com:EdiPNoBS2iYxOsAF9e2ceMbk"}, 
 "user": "apimanager@taurusseo.com"}

What is the quick and easy way to parse through the (pure text) response given above? 通过上面给出的(纯文本)响应进行解析的快速简便的方法是什么?

Also, if the response is a JSON object, then how do I parse through the response, preferably using GSON library? 另外,如果响应是JSON对象,那么如何解析响应,最好使用GSON库? I just want to use a library that is supported by Google App Engine for Java and GSON is one of those. 我只想使用Google App Engine for Java支持的库,而GSON就是其中之一。

Finally, if a response analogous to the above is an XML response, then do I have to model a struct that resembles the response above? 最后,如果类似于上述的响应是XML响应,那么我是否必须对类似于上面的响应的结构建模? Again, is there a quick and easy way to get the response and parse through it, preferably using XML RPC Client? 同样,是否有一种快速简便的方法来获取响应并通过它进行解析,最好使用XML RPC Client? Again, I just want to use a library that is supported by Google App Engine for Java. 同样,我只想使用Google App Engine for Java支持的库。 XML RPC client is just a suggestion, if you can suggest something better then kindly do so. XML RPC客户端只是一个建议,如果您可以提出更好的建议,那么请这样做。

I am confused as to why you would get back different data types of responses for the same request. 对于您为什么要针对同一请求返回不同数据类型的响应,我感到困惑。 However, you should be able to inspect the Content-Type header of the response, to determine if you should parse it as JSON or XML. 但是,您应该能够检查响应的Content-Type标头,以确定是否应将其解析为JSON或XML。 As you already mentioned, you could use a library such as Gson to handle the response as JSON, or one of the build-in XML APIs (eg JAXP) to handle the response as XML. 如您已经提到的,您可以使用Gson之类的库将响应作为JSON处理,也可以使用内置XML API(例如JAXP)之一将响应作为XML处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM