Java中構造和解析JSON

來源:互聯網
上載者:User

標籤:ima   oci   ons   格式   asc   cep   數組   try   except   

什麼是 Json?

  JSON(JvaScript Object Notation)(官網網站:http://www.json.org/)是 一種輕量級的資料交換格式。 
  易於人閱讀和編寫。同時也易於機器解析和產生。它基於 JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999 的一個子集。 
  JSON 採用完全獨立於語言的文字格式設定,但是也使用了類似於 C 語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 這些特性使 JSON 成為理想的資料交換語言。

JSON 的兩種結構
 1.“名稱/值”對的集合(A collection of name/value pairs)
  不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),雜湊表 (hash table),有鍵列表(keyed list),或者關聯陣列 (associative array)。 
  在 Java 語言中,我們可以將它理解成 HashMap。
  對象是一個無序的"‘名稱/值‘對"集合。一個對象以"{"(左括弧)開始,"}"(右括弧)結束。每個“名稱”後跟一個":"(冒號);"‘名稱/值‘ 對"之間使用","(逗號)分隔。
  樣本:var json = {"name":"Jack","age":90,"Marray":true};

  

 2. 值的有序列表(An ordered list of values)
  在大部分語言中,它被理解為數組(Array 或 List)。
  數組是值(value)的有序集合。一個數組以"["(左中括弧)開始,"]"(右中括弧)結束。值之間使用","(逗號)分隔。
  樣本:var json = ["Jack","Rose","Tom",89,true,false];

   

 Java中對json的操作的方式:org.json.jar

/**     * 解析JSON     */    public static void ParsingJSon() { String json = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":{\"GD\":{\"province\":\"廣東\",\"province_code\":\"GD\",\"citys\":[{" + "\"city_name\":\"梅州\",\"city_code\":\"GD_MEIZHOU\",\"abbr\":\"粵\"}," + "{\"city_name\":\"汕尾\",\"city_code\":\"GD_SHANWEI\",\"abbr\":\"粵\"}]}},\"error_code\":0}"; try { JSONObject jsonobject = new JSONObject(json); String resultcode = jsonobject.getString("resultcode"); JSONObject jsonone = jsonobject.getJSONObject("result"); JSONObject jsontwo = jsonone.getJSONObject("GD"); JSONArray jsonarray = jsontwo.getJSONArray("citys"); for (int i = 0; i < jsonarray.length(); i++) { JSONObject ject = jsonarray.getJSONObject(i); String s1 = ject.getString("city_name"); String s2 = ject.getString("city_code"); String s3 = ject.getString("abbr"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/**     * 構造JSON     */    public static void ConstructingJSon() { JSONObject jsonFirst = new JSONObject(); JSONObject jsonSecond = new JSONObject(); JSONObject jsonThird = new JSONObject(); try { JSONArray citys = new JSONArray(); JSONObject cityone = new JSONObject(); JSONObject citytwo = new JSONObject(); cityone.put("city_name", "梅州"); cityone.put("city_code", "GD_MEIZHOU"); cityone.put("abbr", "粵"); citytwo.put("city_name", "汕尾"); citytwo.put("city_code", "GD_SHANWEI"); citytwo.put("abbr", "粵"); citys.put(cityone); citys.put(citytwo); jsonThird.put("province", "廣東"); jsonThird.put("province_code", "GD"); jsonThird.put("citys", citys); jsonSecond.put("GD", jsonThird); jsonFirst.put("resultcode", "200"); jsonFirst.put("reason", "成功的返回"); jsonFirst.put("result", jsonSecond); jsonFirst.put("error_code", 0); } catch (Exception e) { e.printStackTrace(); } System.out.println(jsonFirst.toString()); }

 

Java中構造和解析JSON

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.