gpt4 book ai didi

java - 读取解析 JSON

转载 作者:行者123 更新时间:2023-12-01 06:54:51 24 4
gpt4 key购买 nike

我有一个 JSON 字符串

   [
{"created_at":"Thu Jan 24 07:27:12 +0000 2013",
"id":294345590119227392,
"user":{
"id":213539531,
"name":"Hamid",
"screen_name":"HamidMirGEO",
},
"retweeted":false
}]

使用以下代码解析

        InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
byte [] buffer = new byte[is.available()];
while (is.read(buffer) != -1);
String jsontext = new String(buffer);
JSONArray entries = new JSONArray(jsontext);
for (i=0;i<entries.length();i++)
{
JSONObject post = entries.getJSONObject(i);
x += "Date:" + post.getString("created_at") + "\n";
x += "Post:" + post.getString("text") + "\n\n";
x += "Pp:" + post.getString("screen_name") + "\n\n";//error reading this
}

成功解析前两个,但在读取第二个大括号 {} 中的数据时遇到问题;任何解决方案。

最佳答案

用途:

x += "Pp:" + post.getJSONObject("user").getString("screen_name") + "\n\n";

而不是:

x += "Pp:" + post.getString("screen_name") + "\n\n";

关于java - 读取解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com