<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 1. JSON基礎 JSON(JavaScript Object Notation, JS 對象簡譜) 是一種輕量級的數據交換格式。它基于 ECMAScript (歐洲計算機協會制定的js規范)的一個子集,采用完全獨立于編程語言的文本格式來存儲和表示數據。簡潔和清晰的層次結構使得 JSON 成為理想的數據交換語言。 易于人閱讀和編寫,同時也易于機器解析和生成,并有效地提升網絡傳輸效率。 ### 1.1 JSON數據類型 1、數字(整型、浮點數、定點數); 2、字符和字符串; 3、布爾類型。 還有其他數據類型: 一、對象; 二、null; 三、數組。 ## 2. fastjson ### 2.1 FastJson的特點: 1.FastJson數度快,無論序列化和反序列化,都是當之無愧的fast 2.功能強大(支持普通JDK類包括任意Java Bean Class、Collection、Map、Date或enum) 3.零依賴(沒有依賴其它任何類庫) 1.3.FastJson的簡單說明: **FastJson對于json格式字符串的解析主要用到了下面三個類:** 1.JSON:fastJson的解析器,用于JSON格式字符串與JSON對象及javaBean之間的轉換 2.JSONObject:fastJson提供的json對象,可以將java字符串轉化成json對象 3.JSONArray:fastJson提供json數組對象 ### 2.2 使用 #### 2.2.1 String轉Json對象 依賴: ``` ``` 1. 字符串 ~~~ private static final String JSON_OBJ_STR = "{\"studentName\":\"tuna\",\"studentAge\":28}"; ~~~ 2. JSONObject.parseObject(String) 轉換 ~~~ @Test public void testJSONStrToJSONObject() { JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); System.out.println("================將字符串轉成json對象================"); System.out.println("studentName: " + jsonObject.getString("studentName") + ":" + " studentAge: " + jsonObject.getInteger("studentAge")); String jsonString = jsonObject.toJSONString(); System.out.println("================將json對象轉成字符串================"); System.out.println(jsonString); } ~~~ #### 2.2.2 String轉成javabean 1. 實體類 ~~~ package com.aixin.tuna.json; /** * Created by dailin on 2018/7/10. */ public class Student { private String studentName; private int studentAge; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } @Override public String toString() { return "Student{" + "studentName='" + studentName + '\'' + ", studentAge=" + studentAge + '}'; } } ~~~ 2. 轉換 JSONObject.parseObject(String,Class); ~~~ private static final String JSON_OBJ_STR = "{\"studentName\":\"tuna\",\"studentAge\":28}"; Student student = JSONObject.parseObject(JSON_OBJ_STR,Student.class); System.out.println(student.toString()); ~~~ #### 2.2.3 String轉成Map ~~~ @Test public void test2(){ Map<String, Object> paramsListMap = JSON.parseObject(JSON_OBJ_STR,new TypeReference<Map<String, Object>>(){}); Set<Map.Entry<String, Object>> entries = paramsListMap.entrySet(); for (Map.Entry<String,Object> v:entries){ System.out.println(v.getKey() + ":" + v.getValue()); } } ~~~ 輸出 ~~~ {studentAge=28, studentName=tuna} studentAge:28 studentName:tuna ~~~ #### 2.2.4 javabean轉Json字符串 ~~~ @Test public void test3(){ Student student = new Student("tuna", 12); String jsonString = JSONObject.toJSONString(student); System.out.println(jsonString); } ~~~ ~~~ {"studentAge":12,"studentName":"tuna"} ~~~ #### 2.2.5 JavaList到JsonArray的轉換 數組字符串轉JsonArray,J ~~~ /** * JavaList到JsonArray的轉換 */ @Test public void testJavaListToJsonArray() { //已知JavaList Student student = new Student("tuna", 12); Student studenttwo = new Student("huanhuan", 15); List<Student> students = new ArrayList<Student>(); students.add(student); students.add(studenttwo); //方式一 String jsonString = JSONArray.toJSONString(students); JSONArray jsonArray = JSONArray.parseArray(jsonString); System.out.println(jsonArray); //方式二 JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students); System.out.println(jsonArray1); } ~~~ 輸出 ~~~ [{"studentAge":12,"studentName":"tuna"},{"studentAge":15,"studentName":"huanhuan"}] [{"studentAge":12,"studentName":"tuna"},{"studentAge":15,"studentName":"huanhuan"}] ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看