- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我用 Java 写的:
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 Example1 {
public static String readReddit()throws IOException, URISyntaxException {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"));
JSONObject jsonObject = (JSONObject) obj;
String kind = (String) jsonObject.get("kind");
JSONArray data = (JSONArray) jsonObject.get("data");
System.out.println("Kind:" + kind);
System.out.println("Data:");
Iterator<String> iterator = data.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
在 Maven 中使用这些依赖项:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
我想读取这个 JSON 文件:
[{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"banned_by": null,
"media_embed": {
},
"subreddit": "PoliticalDiscussion",
"selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>The first Presidential Debate for the 2016 Elections will air tonight at 9:00PM Eastern at Hofstra University in New York. The debate will feature Donald Trump and Hillary Clinton in their first head-to-head televised debate. The debate will be 90 minutes and commercial free.</p>\n\n<p>The Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will <a href=\"https://twitter.com/CBSNews/status/780506360257675264\">receive the first question</a>.</p>\n\n<hr/>\n\n<h2><strong>Where to Watch</strong></h2>\n\n<p>Unlike the primaries, these debates are hosted by the Commission on Presidential Debates and the broadcast is not restricted to one network, making for a wide range of viewing options. Here are just a few of the ways you can watch:</p>\n\n<p><strong>Television:</strong></p>\n\n<p>The event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.</p>\n\n<p><strong>Live Streams:</strong></p>\n\n<p><a href=\"https://www.c-span.org/2016presidentialDebates/\">C-SPAN</a></p>\n\n<p><a href=\"https://www.youtube.com/watch?v=L6QElWIKfDk\">Washington Post (YouTube)</a></p>\n\n<p><a href=\"https://www.youtube.com/watch?v=EuHuzhzb1nc\">PBS (YouTube)</a></p>\n\n<p><a href=\"https://www.youtube.com/watch?v=855Am6ovK7s\">NBC (YouTube)</a></p>\n\n<p><a href=\"https://twitter.com/i/live/778347749217406976\">Bloomberg Politics (Twitter with real-time fact checking)</a></p>\n\n<p><a href=\"https://www.facebook.com/ABCNews/\">ABC (Facebook)</a></p>\n\n<hr/>\n\n<h2><strong>Live Discussion</strong></h2>\n\n<p>If you&#39;d like to participate in real-time discussion, we encourage you to head over to <a href=\"https://discord.gg/caAhhCv\">our Discord server</a> and check out our <a href=\"https://discordapp.com/channels/154635757615710208/229738984618721280\">Presidential Debate Channel</a>. </p>\n\n<hr/>\n\n<p>Please use this thread to discuss your tonight&#39;s debate as it happens. All subreddit rules still apply in live threads and in Discord, so please remember to remain civil and try to keep discussion quality high. <strong>This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!</strong></p>\n</div><!-- SC_ON -->",
"selftext": "The first Presidential Debate for the 2016 Elections will air tonight at 9:00PM Eastern at Hofstra University in New York. The debate will feature Donald Trump and Hillary Clinton in their first head-to-head televised debate. The debate will be 90 minutes and commercial free.\n\nThe Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will [receive the first question](https://twitter.com/CBSNews/status/780506360257675264).\n\n-----\n\n**Where to Watch**\n-\n\nUnlike the primaries, these debates are hosted by the Commission on Presidential Debates and the broadcast is not restricted to one network, making for a wide range of viewing options. Here are just a few of the ways you can watch:\n\n**Television:**\n\nThe event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.\n\n**Live Streams:**\n\n[C-SPAN](https://www.c-span.org/2016presidentialDebates/)\n\n[Washington Post (YouTube)](https://www.youtube.com/watch?v=L6QElWIKfDk)\n\n[PBS (YouTube)](https://www.youtube.com/watch?v=EuHuzhzb1nc)\n\n[NBC (YouTube)](https://www.youtube.com/watch?v=855Am6ovK7s)\n\n[Bloomberg Politics (Twitter with real-time fact checking)](https://twitter.com/i/live/778347749217406976)\n\n[ABC (Facebook)](https://www.facebook.com/ABCNews/)\n\n------\n\n**Live Discussion**\n-\n\nIf you'd like to participate in real-time discussion, we encourage you to head over to [our Discord server](https://discord.gg/caAhhCv) and check out our [Presidential Debate Channel](https://discordapp.com/channels/154635757615710208/229738984618721280). \n\n----------\n\nPlease use this thread to discuss your tonight's debate as it happens. All subreddit rules still apply in live threads and in Discord, so please remember to remain civil and try to keep discussion quality high. **This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!**",
"likes": null,
"suggested_sort": "new",
"user_reports": [],
"secure_media": null,
"saved": false,
"id": "54nuut",
"gilded": 0,
"secure_media_embed": {
},
"clicked": false,
"report_reasons": null,
"author": "BagOnuts",
"media": null,
"score": 550,
"approved_by": null,
"over_18": false,
"domain": "self.PoliticalDiscussion",
"hidden": false,
"num_comments": 8576,
"thumbnail": "",
"subreddit_id": "t5_2sfmf",
"edited": 1474937730,
"link_flair_css_class": "green2",
"author_flair_css_class": "",
"downs": 0,
"archived": false,
"removal_reason": null,
"stickied": false,
"is_self": true,
"hide_score": false,
"permalink": "/r/PoliticalDiscussion/comments/54nuut/debate_megathread_live_discussion_thread_for_the/",
"locked": false,
"name": "t3_54nuut",
"created": 1474964458,
"url": "https://www.reddit.com/r/PoliticalDiscussion/comments/54nuut/debate_megathread_live_discussion_thread_for_the/",
"author_flair_text": "Extra Nutty",
"quarantine": false,
"title": "[Debate Megathread] LIVE Discussion Thread for the First Presidential Debate of 2016",
"created_utc": 1474935658,
"link_flair_text": "Presidential Debate",
"ups": 550,
"upvote_ratio": 0.83,
"mod_reports": [],
"visited": false,
"num_reports": null,
"distinguished": null
}
}],
"after": null,
"before": null
}
},
{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
..
但我收到错误:
Error:(132, 32) java: no suitable method found for parse(java.io.InputStream)
method org.json.simple.parser.JSONParser.parse(java.lang.String) is not applicable
(argument mismatch; java.io.InputStream cannot be converted to java.lang.String)
method org.json.simple.parser.JSONParser.parse(java.io.Reader) is not applicable
(argument mismatch; java.io.InputStream cannot be converted to java.io.Reader)
没找到问题出在哪里。另外,我不仅仅想打印得到的这些结果,而是保存它们并使用。我对 Java 没有太多经验。任何帮助将不胜感激。也许我没有导入任何依赖项?
最佳答案
您可以尝试一下下面的代码吗?
ClassLoader classLoader = Example1.class.getClassLoader();
File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());
Object obj = parser.parse(new FileReader(file));
关于java - 找不到合适的方法来解析(java.io.InputStream),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250905/
假设我正在使用 segues 转换 View Controller 。我在 View Controller 1 上有一个 textField,在第二个 View Controller 上有一个标签。当
在下面的代码中,当我在表中插入数据时,回滚的目的是什么,如果我想回滚,我不应该插入它,那么使用回滚的合适方法是什么? BEGIN TRANSACTION Insert into dimCustomr
我一直在阅读一些帖子,并想知道是否有人可以介绍 TrieMap 何时比使用 HashMap 更可取的情况。 那么本质上是什么架构决策应该激励使用 TrieMap? 最佳答案 根据文档。 它是可以在多线
什么时候 do-while 比其他类型的循环更好?有哪些常见场景比其他场景更好? 我了解 do-while 的功能,但不了解何时使用它。 最佳答案 当您需要至少完成一次某事,但不知道启动循环之前的次数
fileExistsAtPath 的文档如下: Attempting to predicate behavior based on the current state of the file syst
当 XCode 分析我的代码时,它发现了潜在的内存泄漏。我使用 ARC,但我了解到 ARC 不处理 C 类型。因为我使用 CGImageRef 来创建 UIImage 并分配给 UIImageView
我有一个每天更新一次的大型数据集。我正在缓存对该数据进行昂贵查询的结果,但我想每天更新该缓存。我正在考虑使用 CacheItemRemovedCallback 每天重新加载我的缓存,但我有以下问题:
我了解 IoC 容器是什么,并且一直在阅读结构图。该技术似乎很容易使用。我的问题是,使用 IoC 容器的适当粒度级别是多少? 我看到以下可能的 IoC 应用级别: 打破所有对象之间的所有依赖关系——当
我用 Java 编写了一个应用程序。我从数据库中获取一个表(客户端),其中包含以下字段: 名称 |姓氏 |地址 在我的应用中存储这些数据的最佳解决方案是什么?我应该为每个客户端创建一个对象并将这些对象
这个问题在这里已经有了答案: Use of 'prototype' vs. 'this' in JavaScript? (16 个答案) 关闭 8 年前。 function A() { this
我已经试验了一段时间 asyncio 并阅读了 PEPs ;一些教程;甚至是 O'Reilly book 。 我想我已经掌握了窍门,但我仍然对 loop.close() 的行为感到困惑,我不太清楚何时
它是否正确,因为在 Windows 中并没有说它不好或不推荐。 例如像这样: int APIENTRY _tWinMain(HINSTANCE hInstance,
我在更新我的网站时遇到问题,谷歌搜索结果显示指向旧页面的链接,这些链接现在是 404,其中一些甚至包含已弃用的内容。 我的问题是关于 301 的使用。旧页面具有深层嵌套页面,如下例所示: ww
我使用 JUnit 和 FEST 对我们的应用程序进行 Swing 集成测试,我在测试用例中多次启动和停止。 @after 是否应该包含对 robot.cleanUp() 的调用? 最佳答案 一般规则
我是一名从未真正使用过 .dll 文件的程序员。当然,当我需要第 3 方软件时,例如图形库、帮助我创建图形的库等。我会将引用/ddl 文件添加到我的程序中并在我的代码中使用它们。 此外,您似乎可以将
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我目前正在尝试更多地利用 kotlin 协程。但我面临一个问题:在这些协程中使用 moshi 或 okhttp 时,我收到警告: “不适当的阻塞方法调用” 解决这些问题的最佳方法是什么?我真的不想不合
我有点不确定什么时候适合使用 Html.RenderAction() 来渲染我的 View ,什么时候不适合。我的理解是,因为它不是 ASP.NET MVC 的“官方”组件,所以使用它是不好的做法,它
假设你想开发你的 Controller ,以便你使用 ViewModel 来包含你渲染的 View 的数据,所有数据都应该包含在 ViewModel 中吗?什么条件下可以绕过 ViewModel? 我
您何时考虑在 .NET 中创建用户控件?您是否有一些基本标准来从页面中排除您的代码并引入新的用户控件? 通常我倾向于遵循这些来决定我是否需要用户控件: 当使用单独的用户控件使页面看起来更具可读性时 当
我是一名优秀的程序员,十分优秀!