Json数据解析在线网站:https://www.sojson.com/editor.html
1.什么是JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
2.JSON数据格式
JSON数据格式通常两种格式,一种是对象格式的,另一种是数组对象:
{"name":"JSON","address":"北京市西城区","age":25 }
[{"name":"JSON","address":"北京市西城区","age":25}]
3.关键类
org.json.JSONArray
org.json.JSONObject
JSONObject:用 { } 来表示
例如: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "content" : null }
JSONArray:用 [ { } , { } , ...... , { } ] 来表示
例如: [ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;
4.用法
package com.example.jsonanaly; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); parseJson(); } //从本地文件assets/test.json中解析Json格式数据 private void parseJson(){ try { // InputStream is = this.getAssets().open("test.json");//eclipse InputStream is = MainActivity.this.getClass().getClassLoader().getResourceAsStream("assets/" + "text.json");//android studio BufferedReader bufr = new BufferedReader(new InputStreamReader(is)); String line; StringBuilder builder = new StringBuilder(); while ((line = bufr.readLine()) != null) { builder.append(line); } is.close(); bufr.close(); try { JSONObject root = new JSONObject(builder.toString()); Log.d("info","cat=" + root.getString("cat")); JSONArray array = root.getJSONArray("languages"); for (int i = 0; i < array.length(); i++) { JSONObject lan = array.getJSONObject(i); Log.d("info","-----------------------"); Log.d("info","id=" + lan.getInt("id")); Log.d("info","ide=" + lan.getString("ide")); Log.d("info","name=" + lan.getString("name")); } } catch (JSONException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //Json数据的创建 try { JSONObject root = new JSONObject(); root.put("cat", "it"); JSONObject lan1 = new JSONObject(); lan1.put("id", 1); lan1.put("ide", "Eclipse"); lan1.put("name", "Java"); JSONObject lan2 = new JSONObject(); lan2.put("id", 2); lan2.put("ide", "XCode"); lan2.put("name", "Swift"); JSONObject lan3 = new JSONObject(); lan3.put("id", 3); lan3.put("ide", "Visual Studio"); lan3.put("name", "C#"); JSONArray array = new JSONArray(); array.put(lan1); array.put(lan2); array.put(lan3); root.put("languages", array); Log.d("info",root.toString()); } catch (JSONException e) { e.printStackTrace(); } } }