android json解析及简单例子
封面图
•
2025-10-24
•
Android开发
177
0
0
0
在 Android 开发中,JSON(JavaScript Object Notation)是一种非常常用的轻量级数据交换格式。Android 提供了 `org.json` 包来解析和生成 JSON 数据。 --- ## 一、JSON 简介 JSON 是一种基于文本的结构化数据格式,常用在客户端与服务器之间传输数据。例如: ```json { "name": "张三", "age": 25, "isStudent": false, "hobbies": ["篮球", "阅读"], "address": { "city": "北京", "country": "中国" } } ``` --- ## 二、Android 中使用 JSON 解析 ### 1. 使用 `JSONObject` 和 `JSONArray` #### 示例:解析一个简单的 JSON 字符串 ```java String json = "{ \"name\": \"张三\", \"age\": 25, \"isStudent\": false }"; try { JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); boolean isStudent = jsonObject.getBoolean("isStudent"); Log.d("JSON解析结果", "姓名: " + name); Log.d("JSON解析结果", "年龄: " + age); Log.d("JSON解析结果", "是否学生: " + isStudent); } catch (JSONException e) { e.printStackTrace(); } ``` --- ### 2. 解析嵌套 JSON 对象 ```json { "name": "李四", "age": 30, "address": { "city": "上海", "postcode": "200000" } } ``` ```java String json = "{ \"name\": \"李四\", \"age\": 30, \"address\": { \"city\": \"上海\", \"postcode\": \"200000\" } }"; try { JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); JSONObject address = jsonObject.getJSONObject("address"); String city = address.getString("city"); String postcode = address.getString("postcode"); Log.d("JSON解析结果", "姓名: " + name); Log.d("JSON解析结果", "城市: " + city); Log.d("JSON解析结果", "邮编: " + postcode); } catch (JSONException e) { e.printStackTrace(); } ``` --- ### 3. 解析 JSON 数组(JSONArray) ```json { "students": [ { "name": "张三", "age": 20 }, { "name": "李四", "age": 22 } ] } ``` ```java String json = "{ \"students\": [ { \"name\": \"张三\", \"age\": 20 }, { \"name\": \"李四\", \"age\": 22 } ] }"; try { JSONObject jsonObject = new JSONObject(json); JSONArray students = jsonObject.getJSONArray("students"); for (int i = 0; i < students.length(); i++) { JSONObject student = students.getJSONObject(i); String name = student.getString("name"); int age = student.getInt("age"); Log.d("JSON解析结果", "学生: " + name + ", 年龄: " + age); } } catch (JSONException e) { e.printStackTrace(); } ``` --- ## 三、注意事项 - 使用 `JSONObject` 和 `JSONArray` 需要导入 `org.json.*` 包。 - 在 Android 中,通常不需要额外添加依赖(因为默认已包含)。 - 如果从网络获取 JSON 数据,建议放在子线程中处理(如使用 `AsyncTask` 或 `Thread`)。 --- ## 四、示例项目结构 假设你有一个 Activity,用于解析 JSON 数据并显示结果: ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String json = "{ \"name\": \"王五\", \"age\": 28, \"isStudent\": true }"; try { JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); boolean isStudent = jsonObject.getBoolean("isStudent"); TextView textView = findViewById(R.id.textView); textView.setText("姓名: " + name + "\n年龄: " + age + "\n是否学生: " + isStudent); } catch (JSONException e) { e.printStackTrace(); } } } ``` --- ## 五、总结 | 类名 | 功能 | |--------------|--------------------------| | `JSONObject` | 解析 JSON 对象 | | `JSONArray` | 解析 JSON 数组 | | `getString()` | 获取字符串值 | | `getInt()` | 获取整数值 | | `getBoolean()`| 获取布尔值 | | `getJSONObject()` | 获取嵌套对象 | --- 如果你有具体的 JSON 数据或需要解析 API 返回的数据,可以贴出来,我可以帮你写对应的解析代码 😊
上一篇:
android vector pathData绘制自己的vectordrawable
下一篇:
Android imageView图片按比例缩放
标题录入,一次不能超过6条
留言
评论