原文地址:http://hi.baidu.com/chunying031/item/054f60ab431abcab28ce9de5
packageno.integrasco.ingentia.news.qaedition;
publicclassPerson{
privateStringname;
privateintage;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
}
packageno.integrasco.ingentia.news.qaedition;
importjava.util.List;
importnet.sf.json.JSONArray;
importnet.sf.json.JsonConfig;
publicclassJsonTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//转换方法1
JSONArrayarray=JSONArray.fromObject("[{'name':'hehe','age':22}]");
List<Person>list=JSONArray.toList(array,Person.class);//过时方法
System.out.println(list.get(0).getName());
//转换方法2
List<?>list2=JSONArray.toList(array,newPerson(),newJsonConfig());//参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据
Personperson=(Person)list2.get(0);
System.out.println(person.getAge());
}
}
从页面接收json格式的数据,在java类里面进行解析
StringjsonStr={"name":"zfj","dd":[{"ddr":"1","encrypt":"2","ff":"1","length":"23","ffe":"editStyled","ill":"1","pkor":"2","name":"zfj","isKey":"2","alias":"ffff"}],"addRelations":[{"type":"2","ld":"zfj","ld":"2"}]};
将此字符串放在ruquest或者其他的变量里面,传递到后台。
如放在reques中:
request.getRequestDispatcher("/geServlet?data="+jsonStr).forward(request,response);
在servlet或java类里这样接收:
Stringjson=request.ge[size=large][/size]tParameter("data");
JSONObjectjsonObject=JSONObject.fromObject(data);
Stringname=jsonObject.getString(“name”);
......
//对于数组这样接收用
JSONArrayjan=(JSONArray)jsonObject.get("dd");
if(jan!=null||jan.size()!=0){
for(inti=0;i<ja.size();i++){
JSONObjectjo=JSONObject.fromObject(ja.get(i));
Stringff=jo.getString("ff");
......
}
}
因为我包中没有fromObject、toList,所以用了下面的方法:
importcom.alibaba.fastjson.JSON;
.
ArrayList<SystemMessageInfo>sysMessageInfoList=JSON.parSEObject(resObj,
newTypeReference<ArrayList<SystemMessageInfo>>(){
});
完整的:
1.
importcom.alibaba.fastjson.JSON;
publicStringGetMultiLangMessage(Stringkey,StringdefaultString){
try{
if(multiLangMsg.getItemObject()==null){
sysMsgMap=newHashMap<Integer,SystemMessageInfo>();
LinkedHashMap<String,Object>params=newLinkedHashMap<String,Object>();
params.put("clientName",Session.current.getComputerName());
StringresObj=KSOAPServiceUtil.callWCFService(
"GetSystemMessageForList",params);
Log.e("GetMultiLangMessageresObj",resObj);
if(!PosEnvironment.isServiceReturnNull(resObj)){
multiLangMsg.setItemObject("msg");
}
if(PosEnvironment.isServiceReturnNull(resObj)){
returndefaultString;
else
{
for(SystemMessageInfosystemMessageInfo:sysMessageInfoList){
sysMsgMap.put(systemMessageInfo.getSysMegNumber(),systemMessageInfo);
StringstrValString="";
sysMsgInfo=sysMsgMap.get(Integer.valueOf(key));
if(sysMsgInfo!=null){
strValString=PosEnvironment.getCurrentName(sysMsgInfo.getSysMegName1(),arial; font-size:14px; line-height:21px"> sysMsgInfo.getSysMegName2(),arial; font-size:14px; line-height:21px"> sysMsgInfo.getSysMegName3(),arial; font-size:14px; line-height:21px"> sysMsgInfo.getSysMegName4(),arial; font-size:14px; line-height:21px"> sysMsgInfo.getSysMegName5());
if(PosEnvironment.isNullOrEmpty(strValString)){
strValString=defaultString;
returnstrValString;
}catch(Exceptione){
if(einstanceofNetworkErrorException){
Session.current.getNavigator().getCurrentScreen().ShowMessageBox(
"",e.getMessage(),DialogButtons.OK,newObject[]{POSFEVars.SystemAlertFlag});
else{
2.KSOAPServiceUtil.java
//callingforWCFServicebyusingKSoapjar.
publicclassKSOAPServiceUtil{
publicstaticStringserviceUrl="";
publicstaticStringnamespace="";
publicstaticStringsoap_action_path="";
/**
*theparamsiscallthewebService'sparamsinaccordancewiththeorder
*parameter,notnecessarilyinaccordancewiththeparametername.
*
*@parammethodName
*@paramparams
*@return
*/
publicstaticStringcallWCFService(StringmethodName,Object>params){
StringretSoapObj=null;
SoapObjectrequest=newSoapObject(namespace,methodName);
//Callmethodtosettheparametervalues,thereisnoargumentcan
//be
//omitted
//settheparsmsisjson
if(params!=null&¶ms.size()>0){
for(Map.Entry<String,Object>entry:params.entrySet()){
if(((entry.getValue())instanceofInteger)
||(entry.getValue()instanceofBoolean)
||(entry.getValue()instanceofString)){
request.addProperty(entry.getKey().toString(),arial; font-size:14px; line-height:21px"> entry.getValue());
}else{
JSON.toJSONString(entry.getValue(),SerializerFeature.WriteDateUseDateFormat));
//CallingWebservicemethodgeneratesaSOAPrequestmessage
//settheWebServiceversion
SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet=true;//Here,ifsettoTRUE,thentheserverwill
//getlessthantheparametervalues
//(such
//as:thesedataintothedatabaseinit)
envelope.bodyOut=request;
envelope.encodingStyle="UTF-8";
HttpTransportSEht=newHttpTransportSE(serviceUrl);
envelope.setOutputSoapObject(request);
ht.call(soap_action_path+methodName,envelope);//usingthecall
//methodcall
//WebService
//method
retSoapObj=envelope.getResponse().toString();//WebServicemethod
//toobtainthe
//returnresults
if(retSoapObj.trim().equals("anyType{}")){
retSoapObj="{}";
//calltheWebServicenamespaceandmethod;
returnretSoapObj;
}
原文链接:https://www.f2er.com/json/290524.html