Android Studio json 数据

  JSONObject root =  new  JSONObject();  
             JSONObject cat =  new  JSONObject();  
             cat.put( "cat" "miao" );  
             JSONArray array =  new  JSONArray();  
               
             JSONObject lan1 =  new  JSONObject();  
             lan1.put( "id" 1 ).put( "ide" "Eclispe" ).put( "name" "java" );  
              
             JSONObject lan2 =  new  JSONObject();  
             lan2.put( "id" 2 ).put( "ide" "Xcode" ).put( "name" "Swift" );  
             JSONObject lan3 =  new  JSONObject();  
             lan3.put( "id" 3 ).put( "ide" "Visual Studio" ).put( "name" "C++" );  
             array.put(lan1);  
             array.put(lan2);  
             array.put(lan3);  
               
             root.put( "languages" , array);  
             root.put( "cat" , cat);  
大神博客!

http://www.cnblogs.com/jycboy/p/json_xj.html 


Android Studio中,可以使用JSONObject和JSONArray类来解析JSON数据并显示数据。以下是一个简单的例子: ```java // 假设json是获取的源数据 String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; try { // 将json字符串转换为JSONObject对象 JSONObject jsonObject = new JSONObject(json); // 获取JSONObject中的数据 String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); // 在控制台上打印数据 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); } catch (JSONException e) { e.printStackTrace(); } ``` 如果JSON数据是一个数组,可以使用JSONArray类来解析数据。以下是一个简单的例子: ```java // 假设json是获取的源数据 String json = "[{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}, {\"name\":\"Jane\", \"age\":25, \"city\":\"Los Angeles\"}]"; try { // 将json字符串转换为JSONArray对象 JSONArray jsonArray = new JSONArray(json); // 遍历JSONArray中的每个JSONObject for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); // 获取JSONObject中的数据 String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); // 在控制台上打印数据 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); } } catch (JSONException e) { e.printStackTrace(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值