XML转换到Android的JSON对象 [英] Convert Xml to json object in android
本文介绍了XML转换到Android的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML字符串,如下所示:
I have an Xml String as shown below:
<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?
Is it possible to convert xml to json?
推荐答案
您可以尝试这种方式为好。我曾尝试和测试它自己。
You can try this way as well. I have tried and tested it myself.
步骤1:请下载 java的json.jar
第二步:添加到您的项目 /库
文件夹,然后添加到构建路径
Step 2: Add this to /libs
folder of your project then add to build path.
第三步:然后用它如下:
Step 3: Then use it as follows
-
进口寻找
Imports to look for
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
样品字符串
Sample string
String sampleXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<mobilegate>"
+"<timestamp>232423423423</timestamp>"
+ "<txn>" + "Transaction" + "</txn>"
+ "<amt>" + 0 + "</amt>"
+ "</mobilegate>";
JSON字符串
Json String
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());
输出:
XML:
&LT; XML版本=1.0 encoding="utf-8"?><mobilegate><timestamp>232423423423</timestamp><txn>Transaction</txn><amt>0</amt></mobilegate>$c$c>
JSON:
{mobilegate:{时间戳:232423423423,AMT:0,TXN:交易}}
这篇关于XML转换到Android的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文