json数组的生成,转换与解析

public class json {
public static void main(String[] args) {
//1.生成json
String json1 = createJsonString("name","杰克");
System.out.println("json1:"+json1);

//2.拼装json  , String 用json直接转对象
//JSON解析json格式String,将他转换成对象输出
String json2 = JSON2();
System.out.println("json2:"+json2);

//3解析json String 转json,从中获取元素
String json3 = json3("",json2);
System.out.println("jso3:"+json3);

//4.解析数组
List list = json4("");

}

public static String createJsonString(String key, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(key, value);
        return jsonObject.toString();
    }


@Test
public static String JSON2() {
String ywlx ="qysds";
String ywbm = "sssq";
String ysqxxid = "ysqxxid";
String _query_string_ = null;

StringBuilder sb = new StringBuilder();

sb.append("{\"ywlx\":\"").append(ywlx).append("\",\"ywbm\":\"").append(ywbm.toUpperCase()).append("\",\"ysqxxid\":\"").append(ysqxxid).append("\"");

if (!StringUtils.isEmpty(_query_string_) ){
sb = sb.append(",").append(_query_string_);
}
sb.append("}");

String params = sb.toString();

//System.out.println(sb);

ZlpzCommonReqVO vo = JSON.parseObject(params, ZlpzCommonReqVO.class);
//System.out.println(vo);
return params;
}


public static String json3(String key, String jsonString) {
       // Person person = new Person();
String ywbm = null;
String ysqxxid = null;
String ywlx = null;
        try {
        //1:将String解析出json字符串
        JSONObject jsonobj = JSONObject.parseObject(jsonString); 
        ywbm = jsonobj.getString("ywbm");
        ysqxxid = jsonobj.getString("ysqxxid");
        ywlx = jsonobj.getString("ywlx");
       
        //person.setAddress(ywbm);
        //Person vo = JSON.parseObject(jsonString, Person.class);
            //JSONObject jsonObject = new JSONObject(jsonString);
        //jsonObject.
           /* JSONObject personObject = jsonObject.getJSONObject("person");
            person.setId(personObject.getInt("id"));
            person.setName(personObject.getString("name"));
            person.setAddress(personObject.getString("address"));*/
        } catch (Exception e) {
            // TODO: handle exception
        }
        return ywbm+ysqxxid+ywlx;
        
    }


public static List json4(String jsonString){  
       List personList=new ArrayList();  
       String json =  "[{'id':11,'Name':'jaca','Address':'zzz'},{'id':22,'Name':'jaca','Address':'zzz'},{'id':33,'Name':'jaca','Address':'zzz'}]";
       try {  
           //JSONArray jsonArray=new JSONArray(json);  
           //JSONArray jsonArray = 
        JSONArray jsonArray = JSONArray.parseArray(json);
        //jsonArray.
          // Log.e("ceshi", jsonArray.length()+"长度");  
           for (int i = 0; i < jsonArray.size(); i++) {  
               JSONObject jsonObject= (JSONObject) jsonArray.get(i);  
               System.out.println(jsonObject);
               //personList.add(new Person(jsonObject.getIntValue("id"), jsonObject.getString("name"), jsonObject.getString("age")));  
           }  
           return personList;  
       } catch (Exception e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }  
       return null;  
   }  
    


class Person {

public Person(int i, String string, String string2) {
super();
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
String Name;
String Address;
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Person(String name, String address, int id) {
super();
Name = name;
Address = address;
this.id = id;
}

}


}

你可能感兴趣的:(json数组的生成,转换与解析)