JSON需要的jar包和和String类型转JSON

来源:互联网 发布:算法之美 中文版 pdf 编辑:程序博客网 时间:2024/04/30 13:29

首先声明所有的操作是在java文件中进行的


JSON 需要的jar包

 免费下载地址:

http://download.csdn.net/detail/u011381488/6457097




import java.text.SimpleDateFormat;
import java.util.Date;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;








public class StringToJson {
public static void TestJson(){

//第一种 ------------------------------开始
//(json1={"age":"10","name":"李明"})
//创建JSON类型对象
JSONObject json1=new JSONObject();

//向JSON对象中放入json类型的数据
json1.put("name", "李明1");
json1.put("age", "10");

//输出json1的元素
System.out.println("json1-name="+json1.get("name"));
System.out.println("json1-age="+json1.get("age"));

//输出json1的全部
System.out.println("json1="+json1.toString());
//第一种 ------------------------------结束

//第二种------------------------------开始(json作为一个value被保存到另一个json中:
//json4={"array":[{"age":"10","name":"李明2"},{"age":"10","name":"李明3"}]})
//创建JSON类型对象
JSONObject json2=new JSONObject();
JSONObject json3=new JSONObject();
JSONObject json4 = new JSONObject(); 

//json格式的数组  
JSONArray jsonArray = new JSONArray();


//向JSON对象中放入json类型的数据
json2.put("name", "李明2");
json2.put("age", "10");

json3.put("name", "李明3");
json3.put("age", "10");

//将json放入JSON数组
jsonArray.add(json2);
jsonArray.add(json3);
//jsonArray.set(0, json3);     //是按照

json4.put("array1", jsonArray);
json4.put("array2", jsonArray);

//获取复杂格式的json4的元素
System.out.println("json4="+json4.toString());
System.out.println("json4-getString('array1')="+json4.getString("array1"));
System.out.println("json4-getkay('array1').toString="+json4.get("array1").toString());
System.out.println("json4-getkay('array1').getJSONArray('array1').getJSONObject(0)="+json4.getJSONArray("array1").getJSONObject(0));
System.out.println("json4-getkay('array1').getJSONArray('array1').getJSONObject(0).get('name')="+json4.getJSONArray("array1").getJSONObject(0).get("name"));

//第二种------------------------------结束

//第三种------------------------------开始
//String类型转为json类型
String str1=json4.toString();
JSONObject json5 = JSONObject.fromObject(str1);
System.out.println("json5.toString()="+json5.toString());
//第三种------------------------------结束


//第四种------------------------------开始
String str2="{'name':'李明','age':'10'}";
JSONObject json6=JSONObject.fromObject(str2);
System.out.println("json6.toString()="+json6.toString());
//第四种------------------------------结束

//第五种------------------------------开始
String str3="{\'arraystr3\':[{\"name\":\"李明\",\"age\":\"10\"},{\"name\":\"李明2\",\"age\":\"10\"}]}";
String str4="{\'arraystr3\':[{\'name\':\'李明\',\'age\':\'10\'},{\'name\':\'李明2\',\'age\':\'10\'}]}";

JSONObject json7=JSONObject.fromObject(str3);
JSONObject json8=JSONObject.fromObject(str4);
System.out.println("json7.getJSONArray('arraystr3').getJSONObject(0).toString()="+json7.getJSONArray("arraystr3").getJSONObject(0).toString());
System.out.println("json8.getJSONArray('arraystr3').getJSONObject(0).toString()="+json8.getJSONArray("arraystr3").getJSONObject(0).toString());
System.out.println("json7.toString()="+json7.toString());
//第五种------------------------------结束

}




public static void main(String [] args) throws java.text.ParseException{
TestJson();
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sim2=new SimpleDateFormat("yyyyMMddHHmmss");
Date d1=sim2.parse("20151001000000");
Date d2=new Date();
String d3=sim.format(d2);
System.out.println(d3);


}


}

0 0
原创粉丝点击