JSONArray数据转换成java List

时间:2025-01-26 17:58:56

原文地址:/chunying031/item/054f60ab431abcab28ce9de5

package ;

public class Person {

    private String name;

    private int age;

    public String getName() {

        return name;
    }

    public void setName(String name) {

         = name;
    }

    public int getAge() {

        return age;
    }

    public void setAge(int age) {

         = age;
    }
}

 

package ;

import ;

import ;
import ;

public class JsonTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // 转换方法1
        JSONArray array = ("[{'name':'hehe','age':22}]");
        List<Person> list = (array, );// 过时方法
        ((0).getName());

        // 转换方法2
        List<?> list2 = (array, new Person(), new JsonConfig());//参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据
        Person person = (Person) (0);
        (());
    }

}

 

从页面接收json格式的数据,在java类里面进行解析

String jsonStr = {"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中: 
("/geServlet?data="+jsonStr).forward(request,response); 

在servlet或java类里这样接收: 
String json = [size=large][/size]tParameter("data"); 
JSONObject jsonObject = (data); 
String name = (“name”); 
...... 
//对于数组这样接收用 

JSONArray jan = (JSONArray) ("dd"); 
if(jan!=null||()!=0){ 
for(int i=0;i<();i++){ 
JSONObject jo = ((i)); 
String ff = ("ff"); 
                 ...... 


因为我包中没有fromObject、toList,所以用了下面的方法:


import ;

.

.

.

ArrayList<SystemMessageInfo> sysMessageInfoList = (resObj,

new TypeReference<ArrayList<SystemMessageInfo>>() {

});



完整的:

1.

import ;

public String GetMultiLangMessage(String key, String defaultString) {


try {

if (() == null) {

sysMsgMap=new HashMap<Integer, SystemMessageInfo>();

LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();

("clientName", ());

String resObj = (

"GetSystemMessageForList", params);

("GetMultiLangMessage resObj", resObj);

if (!(resObj)) {

("msg");

}


if((resObj)){

return defaultString;

}

else

{

ArrayList<SystemMessageInfo> sysMessageInfoList = (resObj,

new TypeReference<ArrayList<SystemMessageInfo>>() {

});

for (SystemMessageInfo systemMessageInfo : sysMessageInfoList) {

((), systemMessageInfo);

}

}

}


String strValString = "";

sysMsgInfo = ((key));

if (sysMsgInfo!=null) {

strValString = (sysMsgInfo.getSysMegName1(),

sysMsgInfo.getSysMegName2(),

sysMsgInfo.getSysMegName3(),

sysMsgInfo.getSysMegName4(),

sysMsgInfo.getSysMegName5());

}

if ((strValString)) {

strValString = defaultString;

}

return strValString;

} catch (Exception e) {

if(e instanceof NetworkErrorException){

().getCurrentScreen().ShowMessageBox(

"", (), , new Object[]{});

}

else {

().getCurrentScreen().ShowMessageBox(

"","Application error, please get assistance from IT administrator!", e, );

}

return defaultString;

}


}


//calling for WCF Service by using KSoap jar.

public class KSOAPServiceUtil {


public static String serviceUrl = "";

public static String namespace = "";

public static String soap_action_path = "";


/**

* the params is call the webService's params in accordance with the order

* parameter, not necessarily in accordance with the parameter name.

* @param methodName

* @param params

* @return

*/

public static String callWCFService(String methodName,

LinkedHashMap<String, Object> params) {

String retSoapObj = null;

try {

SoapObject request = new SoapObject(namespace, methodName);

// Call method to set the parameter values, there is no argument can

// be

// omitted

// set the parsms is json

if (params != null && () > 0) {

for (<String, Object> entry : ()) {

if (((()) instanceof Integer)

|| (() instanceof Boolean)

|| (() instanceof String)) {

(().toString(),

());

} else {

(().toString(),

((),));

}

}

}

// Calling Webservice method generates a SOAP request message

// set the WebService version

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

 = true;// Here, if set to TRUE, then the server will

// get less than the parameter values

// (such

// as: these data into the database in it)

 = request;

 = "UTF-8";


HttpTransportSE ht = new HttpTransportSE(serviceUrl);

(request);

(soap_action_path + methodName, envelope);// using the call

// method call

// WebService

// method

retSoapObj = ().toString(); // WebService method

// to obtain the

// return results

if(().equals("anyType{}")){

retSoapObj="{}";

}

} catch (Exception e) {

}

// call the WebService namespace and method;

return retSoapObj;

}


}