<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. java語言

    java中的JSON操作

    時間:2025-03-25 15:28:20 java語言 我要投稿
    • 相關推薦

    java中的JSON操作

        導語:Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。下面我們來看看java中的JSON操作,希望對大家有所幫助。

      一、JSON簡介

      JSON是JavaScript對象表示法,是存儲和交換文本信息的語法。并且獨立于語言和平臺。類似于xml,比xml更小、更快、更易解析。

      二、JSON對象

      1、JSON對象在花括號中書寫,對象可以包含多個名稱/值對:

      {“firstname”:”wang”,”lastname”:”hong”}

      2、JSON數組在方括號中書寫,數組可包含多個對象

    1
    2
    3
    4
    5
    6
    {          
       "employees":[          
           {"firstname":"john","lastname":"doe"},          
           {"firstname":"anna","lastname":"smith"}          
       ]          
    }          

      三、java中讀取json數據

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    import java.io.FileNotFoundException;          
    import java.io.FileReader;          
    import com.google.gson.JsonArray;          
    import com.google.gson.JsonIOException;          
    import com.google.gson.JsonObject;          
    import com.google.gson.JsonParser;          
    import com.google.gson.JsonSyntaxException;          
    public class Json_demo {          
       public static void main(String[] args) throws JsonIOException, JsonSyntaxException, FileNotFoundException {          
           // TODO Auto-generated method stub          
           //建立一個json解析器          
           JsonParser parser = new JsonParser();          
           JsonObject object = (JsonObject) parser.parse(new FileReader("test.json"));          
           //get方法獲得鍵,getAs方法獲得其值          
           System.out.println("cat=" + object.get("cat").getAsString());          
           System.out.println("pop=" + object.get("pop").getAsBoolean());          
           JsonArray array = object.get("languages").getAsJsonArray();          
           for (int i = 0; i < array.size(); i++) {          
               System.out.println("--------------");          
               JsonObject subobject = array.get(i).getAsJsonObject();          
               System.out.println("id=" + subobject.get("id").getAsInt());          
               System.out.println("ide=" + subobject.get("ide").getAsString());          
               System.out.println("name" + subobject.get("name").getAsString());          
           }          
       }          
    }          

      四、使用json創建數據

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import com.google.gson.JsonArray;          
    import com.google.gson.JsonObject;          
    public class Create_Json {          
       public static void main(String[] args) {          
           JsonObject object = new JsonObject();          
           object.addProperty("cat", "it");          
           JsonArray array = new JsonArray();          
           JsonObject lan1 = new JsonObject();          
           lan1.addProperty("id", 1);          
           lan1.addProperty("name", "java");          
           lan1.addProperty("ide", "eclipse");          
           array.add(lan1);          
           JsonObject lan2 = new JsonObject();          
           lan2.addProperty("id", 2);          
           lan2.addProperty("name", "Swift");          
           lan2.addProperty("ide", "XCode");          
           array.add(lan2);          
           JsonObject lan3 = new JsonObject();          
           lan3.addProperty("id", 3);          
           lan3.addProperty("name", "c#");          
           lan3.addProperty("ide", "VS");          
           array.add(lan3);          
           object.add("languages", array);          
           object.addProperty("pop", true);          
           System.out.println(object);          
       }          
    }          

    【java中的JSON操作】相關文章:

    Java中操作Excel表格方法09-06

    PHP中Json應用09-05

    java中BigDecimal的操作方法詳解06-28

    java操作mongodb基礎10-13

    Java數組操作的方法11-09

    java位操作符的知識10-15

    java日期時間基本操作方法08-08

    Java數組的基本操作方法介紹08-14

    Java數組操作的10大方法07-09

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人