java读取json格式数据_java解析json格式数据

要解析的json数据如下:

/*请求结果-->{"result_count":10,"lastpg":10,"next_offset":1,"entry_list":[{"id":"4316","name_value_list":{"message":{"value":"\u65b0\u6587\u53d1\u8868\uff1aAdministrator\u5728\u60a8\u8d1f\u8d23\u7684\u300e\u4e0a\u6d77\u9500\u552e\u90e8\u6863\u6848\u300f\u677f\u5757\u4e0b,\t\u4e8e2013-11-07 18:11:23\u53d1\u8868\u6587\u7ae0\u3010dsfsd \u3011\uff0c\u8bf7\u60a8\u5173\u6ce8\u3002"},"sender":{"value":"System"},"recipient":{"value":"xiao"},"stamp":{"value":"2013-11-07 18:11"},"received":{"value":"0"},"parentid":{"value":"4316"}}},{"id":"4292","name_value_list":{"message":{"value":"\u6587\u7ae0\u8bc4\u8bba\uff1a\u60a8\u53d1\u8868\u5728\u300e\u4e0a\u6d77\u9500\u552e\u90e8\u6863\u6848\u300f\u677f\u5757\u4e0b\u7684\u3010dsfsdf\u3011\u6587\u7ae0\uff0c\t\u4e8e2013-11-05 18:12:55\u88ab xiao \u8bc4\u8bba\uff0c\u8bf7\u60a8\u5173\u6ce8\u3002"},"sender":{"value":"System"},"recipient":{"value":"xiao"},"stamp":{"value":"2013-11-05 18:12"},"received":{"value":"0"},"parentid":{"value":"4292"}}},{"id":"4291","name_value_list":{"message":{"value":"\u8bc4\u8bba\u5220\u9664\uff1a\u60a8\u53d1\u8868\u5728\u677f\u5757\u4e0b\u7684\u3010dsfsdf\u3011\u6587\u7ae0\u4e2d\u7684\u8bc4\u8bba\uff0c\t\u4e8e2013-11-05 18:12:14\u88abxiao\u5220\u9664\uff0c\u8bf7\u60a8\u5173\u6ce8\u3002"},"sender":{"value":"System"},"recipient":{"value":"xiao"},"stamp":{"value":"2013-11-05 18:12"},"received":{"value":"0"},"parentid":{"value":"4291"}}}......*/

解析JSON数据格式

super.handleMessage(msg);

Bundle data = msg.getData();

String Result = data.getString("Result");//取得json数据

Log.i("mylog","请求结果-->" + Result);

JSONObject jsonObject;

try {

jsonObject = new JSONObject(Result);

JSONArray entry_list = jsonObject.getJSONArray("entry_list"); //取得json数组

lastpg = jsonObject.getInt("lastpg"); //取得json对象中的某个int数据

for (int i = 0; i < entry_list.length(); i++) {

JSONObject result = entry_list.getJSONObject(i);//取得json对象

Log.i("listlog",result.getString("id"));

String name_value_list = result.getString("name_value_list");//取得json对象中的某个String数据

jsonObject = new JSONObject(name_value_list);

JSONObject message = jsonObject.getJSONObject("message");

JSONObject sender = jsonObject.getJSONObject("sender");

JSONObject received = jsonObject.getJSONObject("received");

HashMap map = new HashMap();

map.put("lvw_custom_name", sender.getString("value"));

map.put("lvw_custom_description", message.getString("value"));

map.put("received", received.getString("value"));

map.put("pm_id", result.getString("id"));

data1.add(map);

}

} catch (JSONException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用许多库来解析JSON文件,其中最常用的是Jackson和Gson。以下是使用Gson库读取和处理JSON文件的简单示例: 1. 导入Gson库 ```java import com.google.gson.*; ``` 2. 读取JSON文件 ```java // 从文件中读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); ``` 3. 处理JSON数据 ```java // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject中获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } ``` 完整示例代码: ```java import com.google.gson.*; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class JsonExample { public static void main(String[] args) throws Exception { // 从文件中读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject中获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } // 输出结果 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); System.out.println("State: " + state); System.out.println("Hobbies: " + hobbies); } } ``` 其中,假设JSON文件中的内容如下: ```json { "name": "Alice", "age": 25, "address": { "city": "New York", "state": "NY" }, "hobbies": [ "reading", "painting", "hiking" ] } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值