xmljsonjava
㈠ java開發,json是干什麼的
json其實就是封裝了一種數據格式,它使用了自己定義的標准。主要用來在伺服器和客戶端的瀏覽器進行數據交換。因為我們常用的表單形式提交數據,有諸多的不便,json解決了一些問題。
㈡ 怎麼用java讀取一個http://。。。。。 .xml並返回json數據啊
可以用httpClient 發起一個 get或者post請求然後得到返回的結果再做json的解析即可
httpClient 用法:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
㈢ 在java中怎麼把xml文件轉換成json格式
在java中把xml文件轉換成json格式
1、前提需要jar包:
json-lib-2.4-jdk15.jar 和 xom-1.2.5.jar ,maven 倉庫:
net.sf.json-lib
json-lib
2.4
jdk15
xom
xom
1.2.5
2、代碼部分:
public static JSON xmlToJson(String xml) {
XMLSerializer xmlSerializer = new XMLSerializer();
if(xml!=null && !」「.equals(xml)){
xml = xml.replaceAll(「\r|\n」, 「」);
JSON json = xmlSerializer.read(xml);
return json;
}else{
return null;
}
}
public static void main(String[] args) {
String xmlStr = "<parent><id>01</id<name>marry</name</parent>";
JSON json = xmlToJson(xmlStr);
System.out.println(json.toString());
}
3、結果:
{「id」:」01」,」name」:」marry」}
㈣ 【高分求助】java 中 數組格式的 json 怎麼轉成 xml
首先樓主的json數據有問題。
下面代碼親測,可用,兩個文件放在同級目錄:
Test.java
publicclassTest{
publicstaticvoidmain(String[]args){
ConvertXMLtoJSON();
StringjStr="{"users":{"user":["
+"{"uid":"1","node_id":"2","children":[{"id":"43","value":"14"},{"id":"44","value":"15"}]},"
+"{"uid":"2","node_id":"2","children":[{"id":"45","value":"11"},{"id":"46","value":"11"},{"id":"47","value":"16"}]},"
+"{"uid":"3","node_id":"2","children":{"id":"48","value":"11"}}]}"
+"}";
System.out.println(jsontoXml(jStr));
}
publicstaticStringjsontoXml(Stringjson){
try{
XMLSerializerserializer=newXMLSerializer();
JSONjsonObject=JSONSerializer.toJSON(json);
returnserializer.write(jsonObject);
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}
(){
InputStreamis=Test.class.getResourceAsStream("sample.xml");
Stringxml;
try{
xml=IOUtils.toString(is);
System.out.println(xml);
XMLSerializerxmlSerializer=newXMLSerializer();
JSONjson=xmlSerializer.read(xml);
System.out.println(json.toString(1));
}catch(IOExceptione){
e.printStackTrace();
}
}
}
sample.xml
<?xmlversion="1.0"encoding="utf-8"?>
<o>
<usersclass="object">
<userclass="array">
<eclass="object">
<childrenclass="array">
<eclass="object">
<idtype="string">43</id>
埋蔽<valuetype="string">14</value>
</e>
<eclass="object">
<idtype="string">44</id>
<valuetype="string">15</value>
</e>
</children>
<node_idtype="string">2</node_id>
<uidtype="string">1</uid>
</e>
<eclass="object">
<childrenclass="array">
<eclass="object">
<idtype="string">45</id>
<valuetype="string">11</value>
</e>
<eclass="object">
<idtype="string">46</id>
<valuetype="string">11</value>
</e>
<eclass="object">
沒中<idtype="string">47</id>
<valuetype="string">16</value>
枯液山</e>
</children>
<node_idtype="string">2</node_id>
<uidtype="string">2</uid>
</e>
<eclass="object">
<childrenclass="object">
<idtype="string">48</id>
<valuetype="string">11</value>
</children>
<node_idtype="string">2</node_id>
<uidtype="string">3</uid>
</e>
</user>
</users>
</o>
jar包下載地址參考:http://hanyi366.iteye.com/blog/1162365