zoukankan      html  css  js  c++  java
  • json学习笔记

      1 JSon:Android SDK官方的一个库。
      2           Json是当前行业内使用最广泛的一种数据传输格式,是所有开发人员必备的技能之一。
      3           选择Json,可以作为一种数据返回格式,也可以作为一种数据存储格式。
      4  
      5 什么是Json,有什么优点
      6 
      7           Json (JavaScript Object Notation),是一种与开发语言无关的、轻量级的数据格式,更确切的是,它是一种数据格式或规范,对人来说具有易读、易编写的性质,对于机器来说易于程序解析与生成。
      8 样例:
      9 {
     10  “name”:“Terence”,
     11 “age”:24.8,
     12 “birthday”:“1990-05-06”,
     13 “school”:”HDU”,
     14 “major”:[“计算机”,“挖掘机”],
     15 “has_girlFriend”:false,
     16 “car”:null,
     17 “house”:null,
     18 “comments”:”这是一个注释”
     19 }
     20 
     21 数据表示
     22 
     23 数据结构:Object,Array
     24 基本类型:string,number,truefalsenull
     25 Object:使用{}包含键值对结构,key必须是string类型,value值为其他任何基本类型或者数据结构。
     26 Array:数组使用中括号[]来表示,使用逗号来分割元素。
     27 
     28 Json使用
     29 
     30           在官方网站(http://www.json.org.cn/)上有各种语言的Json包,通过这些包,可以对Json做相应的处理。最常用的就是org.json。
     31 
     32 引入依赖
     33 
     34     <dependency>
     35       <groupId>org.json</groupId>
     36       <artifactId>json</artifactId>
     37       <version>20090211</version>
     38     </dependency>
     39 
     40 使用JsonObject实现Json
     41 
     42     private static voidJSONObject(){
     43         JSONObject terence =newJSONObject();
     44         Object nullObj=null;
     45         try {
     46             terence.put("name","terence");
     47             terence.put("age",25.9);
     48             terence.put("birthday","1996-05-06");
     49             terence.put("school","HDU");
     50             terence.put("major",new String[]{"敲键盘","装13"});
     51             terence.put("has_girlfriend",false);
     52             terence.put("car",nullObj);
     53             terence.put("house",nullObj);
     54             terence.put("comment","注释到底");
     55             System.out.println(terence.toString());
     56        } catch(JSONException e) {
     57            // TODOAuto-generated catch block
     58            e.printStackTrace();
     59        }  
     60     }
     61 
     62 使用Map实现Json
     63 
     64    private static voidcreateJsonByMap()
     65     {
     66        Map<String,Object> terence=new HashMap<String,Object>();
     67        ObjectnullObj=null;
     68        terence.put("name","terence");
     69        terence.put("age",25);
     70        terence.put("birthday","1990-05-06");
     71        terence.put("school","HDU");
     72        terence.put("major",new String[]{"敲键盘","装13"});
     73        terence.put("has_girlfriend",false);
     74        terence.put("car",nullObj);
     75        terence.put("house",nullObj);
     76        terence.put("comment","注释到底");
     77        System.out.println(newJSONObject(terence));
     78     }
     79  
     80 使用Bean实现Json
     81 
     82 Bean Class:
     83 public class DaShen {
     84    private Stringname;
     85    private Stringschool;
     86    private boolean has_girlfriend;
     87    private double age;
     88    private Objectcar;
     89    private Objecthouse;
     90    private String[]major;
     91    private Stringcomment;
     92    private String birthday;
     93 }
     94  
     95 实现:
     96    private static void createJsonByBean()
     97    {
     98       DaShen terence=newDaShen();
     99       terence.setAge(25.9);
    100       terence.setBirthday("1990-5-9");
    101       terence.setSchool("HDU");
    102       terence.setMajor(new String[]{"Computer","qiqiqiqi"});
    103       terence.setHas_girlfriend(false);
    104       terence.setComment("sha,sha,sha,sha……");
    105       terence.setCar(null);
    106       terence.setHouse(null);
    107       System.out.println(new JSONObject(terence));
    108    }
    109 
    110 Json解析
    111 
    112 public class ReadJsonSample {
    113     //反向解析为一个json
    114     public static void main(String[] args)throws Exception{
    115         File file=new File(ReadJsonSample.class.getResource("/data/terence.json").getFile());
    116         String content=FileUtils.readFileToString(file);
    117         JSONObject  jsonObject=new JSONObject(content);
    118         if(!jsonObject.isNull("name"))
    119         {
    120             System.out.println("姓名:"+jsonObject.getString("name"));
    121         }      
    122         System.out.println("年龄:"+jsonObject.getDouble("age"));
    123     }
    124 }
  • 相关阅读:
    查询用户表及表结构
    动态页面静态化(转)
    轻松实现 Killer 级应用 Snap (转)
    继续认识OpenID——Benefits of OpenID (转自http://openid.net/getanopenid/individuals/)
    7 Difference Between RIA and Traditional Web Application(转载)
    解决了MovieClip缩略图问题
    Understanding Flex Print Job from A to Z (彻底了解FlexPrintJob)(精彩转载)
    Using Flex Buildin Print Function(Flex 打印功能系列转载)
    StackOverflow is an OpenIDenabled site??Why OpenID?What is OpenID?
    MovieClip实现拖拽等移动位置的功能
  • 原文地址:https://www.cnblogs.com/chenleixing-2017/p/7647955.html
Copyright © 2011-2022 走看看