我有一个 XML 字符串,如下所示:
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Response
xmlns="http://tempuri.org/"><UserResult><Users xmlns=""><User>
<Message>Success</Message>
<UserId>213213213</UserId>
<FullName>Abc</FullName>
<Roles>
<Role>
<RoleId>23232333</RoleId>
<RoleName>Salesperson</RoleName>
</Role>
</Roles>
</User>
</Users>
</UserResult></Response>
</s:Body>
</s:Envelope>
是否可以将 XML 转换为 JSON?
你也可以尝试这个方法。我自己尝试过并测试过。
第1步:请下载java-json.jar http://www.java2s.com/Code/JarDownload/java/java-json.jar.zip
第 2 步:将其添加到/libs
项目的文件夹,然后添加到构建路径。
第三步:然后使用如下
-
寻找进口产品
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
-
示例字符串
String sampleXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<mobilegate>"
+"<timestamp>232423423423</timestamp>"
+ "<txn>" + "Transaction" + "</txn>"
+ "<amt>" + 0 + "</amt>"
+ "</mobilegate>";
-
Json 字符串
JSONObject jsonObj = null;
try {
jsonObj = XML.toJSONObject(sampleXml);
} catch (JSONException e) {
Log.e("JSON exception", e.getMessage());
e.printStackTrace();
}
Log.d("XML", sampleXml);
Log.d("JSON", jsonObj.toString());
Output:
XML:
<?xml version="1.0" encoding="utf-8"?><mobilegate><timestamp>232423423423</timestamp><txn>Transaction</txn><amt>0</amt></mobilegate>
JSON :
{"mobilegate":{"timestamp":232423423423,"amt":0,"txn":"Transaction"}}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)