zoukankan      html  css  js  c++  java
  • 3、示例(在java中使用JSON)

    1. 教程链接(json-smple1.1.1.jar文件)
      1. 链接:http://pan.baidu.com/s/1qXPbYHm 密码:v0f0
    2. 如何使用java编程语言编码和解码JSON
    3. 首先准备环境以便针对JSON进行java编程
      1. 下载jsonsimple-1.1.1.jar文件
      2. 把jar文件的路径添加到classpath环境变量当中



    1. JSON和java实体的映射
      1. JSON.simple实体映射从左侧到右侧为解码或者解析,实体映射从右侧到左侧为编码
      2. 解码时,
        1. java.util.List的默认具体类是org.simple.JSONArray,
        2. java.util.Map的默认具体类是org.simple.JSONObject
    2. 在java中编码JSON
      1. 使用java.util.HashMap的子类JSONObject编码一个JSON对象,这里并没有提供顺序
      2. 如果需要严格的元素顺序,可以使用JSONValue.toJSONString(map)方法的有序映射实现,比如java.util.LinkedHashMap.
      3. package jlu.yangzs.json;
        
        import org.json.simple.JSONObject;
        class JsonEncodeDemo
        {
        public static void main(String[] args)
        {
        	JSONObject obj = new JSONObject();
        	obj.put("name", "foo");
        	obj.put("num", new Integer(100));
        	obj.put("balance", new Double(1000.21));
        	obj.put("is_vip", new Boolean(true));
        	System.out.print(obj);    
        }
        }  
      4. 运行上面的程序,将会出现下面的结果
        1. {"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}
      5. 下面是另外一个例子,使用java JSONObject展示了JSON对象流
        1. package jlu.yangzs.json;
          
          import java.io.IOException;
          import java.io.StringWriter;
          
          import org.json.simple.JSONObject;
          
          public class JsonTest {
          	public static void main(String[] args) throws IOException {
          		
          		JSONObject jsonObejct=new JSONObject();
          		
          		jsonObejct.put("name", "foo");
          		jsonObejct.put("num",new Integer(100));
          		jsonObejct.put("balance", new Double(100.21));
          		jsonObejct.put("is_vip", new Boolean(true));
          		
          		//下面两句代码比较关键
          		StringWriter out=new StringWriter();
          		jsonObejct.writeJSONString(out);
          		
          		String jsonText=out.toString();
          		System.out.println(jsonText);
          
          		
          	}
          
          }
      6. 运行上面的程序,将会显示下面的结果
        1. {"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}
      7. 在java程序当中解码JSON
        1. 例子当中使用了JSONObject和JSONArray,其中,
          1. JSONObejct就是java.util.Map
          2. JSONArray就是java.util.List
          3. 因此,我们可以使用Map或者是List的标准操作来操作来访问他们
          package jlu.yangzs.jsondecode;
          
          import org.json.simple.JSONArray;
          import org.json.simple.JSONObject;
          import org.json.simple.parser.JSONParser;
          import org.json.simple.parser.ParseException;
          
          public class JsonDecodeDemo {
          	
          	public static void main(String[] args) {
          	
          		JSONParser parser=new JSONParser();
          		
          		String s="[0,{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}]";
          		
          		try {
          			Object obj=parser.parse(s);
          			JSONArray jsonArray=(JSONArray)obj;
          			
          			System.out.println("the 2nd element of array:");
          			System.out.println(jsonArray.get(1));
          			
          			System.out.println();
          			
          			JSONObject jsonObj=(JSONObject)jsonArray.get(1);
          			System.out.println("Field "1"");
          			System.out.println(jsonObj.get(1));
          			
          			s="{}";
          			obj=parser.parse(s);
          			System.out.println(obj);
          			
          			s="[5]";
          			obj=parser.parse(s);
          			System.out.println(obj);
          			
          			s="[5,2]";
          			obj=parser.parse(s);
          			System.out.println(obj);
          			
          			
          		} catch (ParseException e) {
          			
          			e.printStackTrace();
          		}
          		
          	}
          	
          	
          }
          
      8. 运行上面的程序,显示结果如下
        1. the 2nd element of array:
          {"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
          
          Field "1"
          null
          {}
          [5]
          [5,2]




  • 相关阅读:
    find the first t.py file through traversal C: and D:
    gevent simple server
    【python实例】判断是否是回文数
    【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)
    【python实例】统计字符串里大写字母,小写字母和非字母的个数
    【python基础】字符串方法汇总
    【python实例】判断质数:for-break-else
    【python实例】while&for 九九乘法表
    【python实例】水仙花数:每个位上的数值的三次方求和,刚好是这个三位数本身
    【python实例】判断输入年份是否是闰年
  • 原文地址:https://www.cnblogs.com/yangzsnews/p/7505164.html
Copyright © 2011-2022 走看看