Json和xml的拼接和解析-创新互联
package com.minimax.jsonparsedemo;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
try {
//Json字符串的解析
String json="[{'patient_id':'122331','visit_id':'1231'},{'patient_id':'122332','visit_id':'1232'},{'patient_id':'122333','visit_id':'1233'}]";
JSONArray array=new JSONArray(json);
for(int k=0;k map=new HashMap();
map.put("101", "2000001");
map.put("102", "2000001");
map.put("103", "2000001");
map.put("104", "2000001");
map.put("105", "2000001");
map.put("106", "2000001");
map.put("107", "2000001");
map.put("108", "2000001");
map.put("109", "2000001");
// String str=getXMLSendData(map);
String str=getJsonSendData(map);
System.out.println("str..."+str);
// Toast.makeText(this, str, Toast.LENGTH_LONG).show();
tv.setText(str);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public String getJsonSendData(HashMap map){
//Map集合中键是patient_id 值是visit_id
// [{'patient_id','120','visit_id','11122'},{},{}]
StringBuffer requestJson=new StringBuffer();
requestJson.append("[");
Set> set=map.entrySet();
Iterator> ito=set.iterator();
while(ito.hasNext()){
Entry entry=ito.next();
String patient_id=entry.getKey();
String visit_id=entry.getValue();
requestJson.append("{");
requestJson.append("'");
requestJson.append("patient_id");
requestJson.append("'");
requestJson.append(":");
requestJson.append("'");
requestJson.append(patient_id);
requestJson.append("'");
requestJson.append(",");
requestJson.append("'");
requestJson.append("visit_id");
requestJson.append("'");
requestJson.append(":");
requestJson.append("'");
requestJson.append(visit_id);
requestJson.append("'");
requestJson.append("}");
requestJson.append(",");
}
int location=requestJson.lastIndexOf(",");
requestJson.deleteCharAt(location);
requestJson.append("]");
return requestJson.toString();
}
public String getXMLSendData(HashMap map){
//Map集合中键是patient_id 值是visit_id
String requestXML="";
try {
Set> set=map.entrySet();
Iterator> ito=set.iterator();
StringWriter writer=new StringWriter();
XmlSerializer serializer=Xml.newSerializer();
serializer.setOutput(writer);
serializer.startDocument("GBK", true);
serializer.startTag(null, "patients");
while(ito.hasNext()){
Entry entry=ito.next();
String patient_id=entry.getKey();
String visit_id=entry.getValue();
//在这块进行两种可能的操作 xml json
serializer.startTag(null, "patient_id");
serializer.text(patient_id);
serializer.endTag(null,"patient_id");
serializer.startTag(null, "visit_id");
serializer.text(visit_id);
serializer.endTag(null,"visit_id");
}
serializer.endTag(null, "patients");
serializer.endDocument();
requestXML=writer.toString();
} catch (Exception e) {
// TODO: handle exception
}
return requestXML;
}
} 
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:Json和xml的拼接和解析-创新互联
浏览路径:http://www.jxjierui.cn/article/dgghio.html


咨询
建站咨询
