xml套json

xml,json解析思路:

xml套json


xml解析:


xml套json

xml解析1:

xml套json

xml解析2:

xml套json


json解析:

json用法:

xml套json

json格式:

xml套json

json解析图:

xml套json


案例:

LiHuoKeJi :

package LHKJ;



public class LiHuoKeJi {
private String userId;
private String userName;
private String password;
private String phone;
private String userType;
private String userInfo;
private String email;

public LiHuoKeJi(String userId, String userName, String password,
String phone, String userType, String userInfo, String email) {
this.userId = userId;
this.userName = userName;
this.password = password;
this.phone = phone;
this.userType = userType;
this.userInfo = userInfo;
this.email = email;
}


@Override
public String toString() {
return this.userId+" "+this.userName+" "+this.password+" "+this.phone+" "+this.userType+" "+this.userInfo+" "+this.email;
}

}

test:

package LHKJ;


import java.util.Arrays;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.json.JSONObject;
import org.junit.Test;


public class jTest {
@Test
public void test(){
LiHuoKeJi[] lhkj=new LiHuoKeJi[0];
/*解析xml嵌套json*/
String xmljson="<?xml version=\"1.0\" encoding=\"UTF-8\"?><content encode=\"3\" type=\"0\" "
+"result=\"200\">{\"data\":{\"userId\":\"0d5ef23e-7ca5-427c-8b9f-ac7e6d81b317\","
+"\"userName\":\"qin\",\"password\":\"666666\",\"phone\":\"10086\",\"userType\":1,"
+"\"userInfo\":\"test\",\"email\":\"[email protected]\"}}</content>";

try {
Document document = DocumentHelper.parseText(xmljson);
Element root = document.getRootElement();
String json = root.getStringValue();
JSONObject object=new JSONObject(json);
JSONObject totle = object.getJSONObject("data");
String userId = totle.getString("userId");
String userName = totle.getString("userName");
String password = totle.getString("password");
String phone = totle.getString("phone");
String userType = totle.getString("userType");
String userInfo = totle.getString("userInfo");
String email = totle.getString("email");
LiHuoKeJi Lhkj=new LiHuoKeJi(userId, userName, password, phone, userType, userInfo, email);
lhkj=Arrays.copyOf(lhkj, lhkj.length+1);
lhkj[lhkj.length-1]=Lhkj;
} catch (Exception e) {
System.out.println("捕获异常!!!");
e.printStackTrace();
}
for( LiHuoKeJi lh:lhkj){
System.out.println(lh.toString());
}
}
}