我正在尝试解析json文件以使用Java提取某些字段。但是,我正在获取某些字段(标签)的空值,例如国家/地区,site_type,replies_count,partners_count等。我尝试在json对象之后使用.toString(),但我仍然不断获取NULL值。
有人可以帮助我修复代码吗?作为json的示例如下:
{
"posts": [
{
"thread": {
"uuid": "5cf0a3152a01d082500a46b358e984610ce87c9c",
"url": "https://www.cnn.com/videos/cnnmoney/2018/05/27/active-shooter-video-game-backlash.hln",
"site_full": "www.cnn.com",
"site": "cnn.com",
"site_section": "http://feeds.feedburner.com/cnn/wYAn",
"site_categories": [
"media"
],
"section_title": "CNN.com - RSS Channel - HP Hero",
"title": "New 'school shooter' game stirs outrage",
"title_full": "New 'school shooter' game stirs outrage",
"published": "2018-05-27T15:15:00.000+03:00",
"replies_count": 0,
"participants_count": 0,
"site_type": "news",
"country": "US",
"spam_score": 0.0,
"main_image": "https://cdn.cnn.com/cnnnext/dam/assets/180527080429-video-game-active-shooter-super-tease.jpg",
"performance_score": 3,
"domain_rank": 81,
"social": {
"facebook": {
"likes": 399,
"comments": 0,
"shares": 399
},
"gplus": {
"shares": 0
},
"pinterest": {
"shares": 2
},
"linkedin": {
"shares": 0
},
"stumbledupon": {
"shares": 0
},
"vk": {
"shares": 0
}
}
},
"uuid": "5cf0a3152a01d082500a46b358e984610ce87c9c",
"url": "https://www.cnn.com/videos/cnnmoney/2018/05/27/active-shooter-video-game-backlash.hln",
"ord_in_thread": 0,
"author": "",
"published": "2018-05-27T15:15:00.000+03:00",
"title": "New 'school shooter' game stirs outrage",
"text": "MUST WATCH New 'active shooter' video game sparks backlash Thousands of people have petitioned to stop the release of a new video game which takes on the point of view of a school gunman.",
"highlightText": "",
"highlightTitle": "",
"language": "english",
"external_links": [],
"entities": {
"persons": [],
"organizations": [],
"locations": []
},
"rating": null,
"crawled": "2018-05-27T16:05:21.021+03:00"
}
],
"total_count": "122"
这是我的代码(我正在使用json.simple。* API解析json):
import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JSONRetriever {
public static void main(String[] args) {
JSONParser jsonParser = new JSONParser();
Object object;
try {
object = jsonParser.parse(new FileReader("C:\\sample.json"));
JSONObject jsonObject = (JSONObject) object;
JSONArray posts = (JSONArray) jsonObject.get("posts");
Iterator itr = posts.iterator();
while (itr.hasNext()) {
Object slide = itr.next();
JSONObject jsonObject2 = (JSONObject) slide;
JSONObject thread = (JSONObject) jsonObject2.get("thread");
String uuid = (String) thread.get("uuid");
String url = (String) thread.get("url");
String site_full = (String) thread.get("site_full");
String site = (String) thread.get("site");
String site_section = (String) thread.get("site_section");
String section_title = (String) jsonObject2.get("section_title");
String title = (String) jsonObject2.get("title");
String title_full = (String) jsonObject2.get("title_full");
String published = (String) jsonObject2.get("published");
String replies_count = (String) jsonObject2.get("replies_count");
String participants_count = (String) jsonObject2.get("participants_count");
String site_type = (String) jsonObject2.get("site_type");
String country = (String) jsonObject2.get("country");
String spam_score = (String) jsonObject2.get("spam_score");
JSONObject social = (JSONObject) jsonObject2.get("social");
System.out.println("section_title: " + section_title);
System.out.println("title: " + title);
System.out.println("title_full: " + title_full);
System.out.println("published: " + published);
System.out.println("replies_count: " + replies_count);
System.out.println("participants_count: " + participants_count);
System.out.println("site_type: " + site_type);
System.out.println("country: " + country);
System.out.println("spam_score: " + spam_score);
System.out.println("uuid: " + uuid);
System.out.println("url: " + url);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您的字段位于您的“线程”对象中,而不是“ jsonObject2”中,例如,使用:
String country = (String) thread.get("country");