No suitable method found for parse(java.io.InputStream)











up vote
1
down vote

favorite












I wrote this in 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;
}


with these dependencies in maven:



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


and I want to read from this JSON file:



[{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"banned_by": null,
"media_embed": {

},
"subreddit": "PoliticalDiscussion",
"selftext_html": "&lt;!-- SC_OFF --&gt;&lt;div class="md"&gt;&lt;p&gt;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.&lt;/p&gt;nn&lt;p&gt;The Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will &lt;a href="https://twitter.com/CBSNews/status/780506360257675264"&gt;receive the first question&lt;/a&gt;.&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Where to Watch&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;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:&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Television:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;The event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Live Streams:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.c-span.org/2016presidentialDebates/"&gt;C-SPAN&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=L6QElWIKfDk"&gt;Washington Post (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=EuHuzhzb1nc"&gt;PBS (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=855Am6ovK7s"&gt;NBC (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://twitter.com/i/live/778347749217406976"&gt;Bloomberg Politics (Twitter with real-time fact checking)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.facebook.com/ABCNews/"&gt;ABC (Facebook)&lt;/a&gt;&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Live Discussion&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;If you&amp;#39;d like to participate in real-time discussion, we encourage you to head over to &lt;a href="https://discord.gg/caAhhCv"&gt;our Discord server&lt;/a&gt; and check out our &lt;a href="https://discordapp.com/channels/154635757615710208/229738984618721280"&gt;Presidential Debate Channel&lt;/a&gt;. &lt;/p&gt;nn&lt;hr/&gt;nn&lt;p&gt;Please use this thread to discuss your tonight&amp;#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. &lt;strong&gt;This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!&lt;/strong&gt;&lt;/p&gt;n&lt;/div&gt;&lt;!-- SC_ON --&gt;",
"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.nnThe 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).nn-----nn**Where to Watch**n-nnUnlike 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:nn**Television:**nnThe event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.nn**Live Streams:**nn[C-SPAN](https://www.c-span.org/2016presidentialDebates/)nn[Washington Post (YouTube)](https://www.youtube.com/watch?v=L6QElWIKfDk)nn[PBS (YouTube)](https://www.youtube.com/watch?v=EuHuzhzb1nc)nn[NBC (YouTube)](https://www.youtube.com/watch?v=855Am6ovK7s)nn[Bloomberg Politics (Twitter with real-time fact checking)](https://twitter.com/i/live/778347749217406976)nn[ABC (Facebook)](https://www.facebook.com/ABCNews/)nn------nn**Live Discussion**n-nnIf 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). nn----------nnPlease 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": [{
..


but I get an error:




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)




I don't find where the issue is. Also I don't just want to print these results I get, but rather save them and use. I am not very much experienced in Java. Any help would be highly appreciated. Maybe I haven't imported any dependencies?










share|improve this question
























  • The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
    – JB Nizet
    Nov 11 at 16:44












  • @JBNizet but there is such file in my disk. That's the issue!
    – Gentsview
    Nov 11 at 16:45








  • 1




    Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
    – JB Nizet
    Nov 11 at 16:46






  • 1




    To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
    – Thomas Kläger
    Nov 11 at 16:47










  • @JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
    – Gentsview
    Nov 11 at 16:49















up vote
1
down vote

favorite












I wrote this in 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;
}


with these dependencies in maven:



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


and I want to read from this JSON file:



[{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"banned_by": null,
"media_embed": {

},
"subreddit": "PoliticalDiscussion",
"selftext_html": "&lt;!-- SC_OFF --&gt;&lt;div class="md"&gt;&lt;p&gt;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.&lt;/p&gt;nn&lt;p&gt;The Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will &lt;a href="https://twitter.com/CBSNews/status/780506360257675264"&gt;receive the first question&lt;/a&gt;.&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Where to Watch&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;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:&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Television:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;The event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Live Streams:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.c-span.org/2016presidentialDebates/"&gt;C-SPAN&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=L6QElWIKfDk"&gt;Washington Post (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=EuHuzhzb1nc"&gt;PBS (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=855Am6ovK7s"&gt;NBC (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://twitter.com/i/live/778347749217406976"&gt;Bloomberg Politics (Twitter with real-time fact checking)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.facebook.com/ABCNews/"&gt;ABC (Facebook)&lt;/a&gt;&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Live Discussion&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;If you&amp;#39;d like to participate in real-time discussion, we encourage you to head over to &lt;a href="https://discord.gg/caAhhCv"&gt;our Discord server&lt;/a&gt; and check out our &lt;a href="https://discordapp.com/channels/154635757615710208/229738984618721280"&gt;Presidential Debate Channel&lt;/a&gt;. &lt;/p&gt;nn&lt;hr/&gt;nn&lt;p&gt;Please use this thread to discuss your tonight&amp;#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. &lt;strong&gt;This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!&lt;/strong&gt;&lt;/p&gt;n&lt;/div&gt;&lt;!-- SC_ON --&gt;",
"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.nnThe 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).nn-----nn**Where to Watch**n-nnUnlike 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:nn**Television:**nnThe event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.nn**Live Streams:**nn[C-SPAN](https://www.c-span.org/2016presidentialDebates/)nn[Washington Post (YouTube)](https://www.youtube.com/watch?v=L6QElWIKfDk)nn[PBS (YouTube)](https://www.youtube.com/watch?v=EuHuzhzb1nc)nn[NBC (YouTube)](https://www.youtube.com/watch?v=855Am6ovK7s)nn[Bloomberg Politics (Twitter with real-time fact checking)](https://twitter.com/i/live/778347749217406976)nn[ABC (Facebook)](https://www.facebook.com/ABCNews/)nn------nn**Live Discussion**n-nnIf 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). nn----------nnPlease 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": [{
..


but I get an error:




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)




I don't find where the issue is. Also I don't just want to print these results I get, but rather save them and use. I am not very much experienced in Java. Any help would be highly appreciated. Maybe I haven't imported any dependencies?










share|improve this question
























  • The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
    – JB Nizet
    Nov 11 at 16:44












  • @JBNizet but there is such file in my disk. That's the issue!
    – Gentsview
    Nov 11 at 16:45








  • 1




    Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
    – JB Nizet
    Nov 11 at 16:46






  • 1




    To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
    – Thomas Kläger
    Nov 11 at 16:47










  • @JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
    – Gentsview
    Nov 11 at 16:49













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I wrote this in 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;
}


with these dependencies in maven:



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


and I want to read from this JSON file:



[{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"banned_by": null,
"media_embed": {

},
"subreddit": "PoliticalDiscussion",
"selftext_html": "&lt;!-- SC_OFF --&gt;&lt;div class="md"&gt;&lt;p&gt;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.&lt;/p&gt;nn&lt;p&gt;The Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will &lt;a href="https://twitter.com/CBSNews/status/780506360257675264"&gt;receive the first question&lt;/a&gt;.&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Where to Watch&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;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:&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Television:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;The event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Live Streams:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.c-span.org/2016presidentialDebates/"&gt;C-SPAN&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=L6QElWIKfDk"&gt;Washington Post (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=EuHuzhzb1nc"&gt;PBS (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=855Am6ovK7s"&gt;NBC (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://twitter.com/i/live/778347749217406976"&gt;Bloomberg Politics (Twitter with real-time fact checking)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.facebook.com/ABCNews/"&gt;ABC (Facebook)&lt;/a&gt;&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Live Discussion&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;If you&amp;#39;d like to participate in real-time discussion, we encourage you to head over to &lt;a href="https://discord.gg/caAhhCv"&gt;our Discord server&lt;/a&gt; and check out our &lt;a href="https://discordapp.com/channels/154635757615710208/229738984618721280"&gt;Presidential Debate Channel&lt;/a&gt;. &lt;/p&gt;nn&lt;hr/&gt;nn&lt;p&gt;Please use this thread to discuss your tonight&amp;#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. &lt;strong&gt;This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!&lt;/strong&gt;&lt;/p&gt;n&lt;/div&gt;&lt;!-- SC_ON --&gt;",
"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.nnThe 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).nn-----nn**Where to Watch**n-nnUnlike 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:nn**Television:**nnThe event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.nn**Live Streams:**nn[C-SPAN](https://www.c-span.org/2016presidentialDebates/)nn[Washington Post (YouTube)](https://www.youtube.com/watch?v=L6QElWIKfDk)nn[PBS (YouTube)](https://www.youtube.com/watch?v=EuHuzhzb1nc)nn[NBC (YouTube)](https://www.youtube.com/watch?v=855Am6ovK7s)nn[Bloomberg Politics (Twitter with real-time fact checking)](https://twitter.com/i/live/778347749217406976)nn[ABC (Facebook)](https://www.facebook.com/ABCNews/)nn------nn**Live Discussion**n-nnIf 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). nn----------nnPlease 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": [{
..


but I get an error:




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)




I don't find where the issue is. Also I don't just want to print these results I get, but rather save them and use. I am not very much experienced in Java. Any help would be highly appreciated. Maybe I haven't imported any dependencies?










share|improve this question















I wrote this in 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;
}


with these dependencies in maven:



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


and I want to read from this JSON file:



[{
"kind": "Listing",
"data": {
"modhash": "",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"banned_by": null,
"media_embed": {

},
"subreddit": "PoliticalDiscussion",
"selftext_html": "&lt;!-- SC_OFF --&gt;&lt;div class="md"&gt;&lt;p&gt;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.&lt;/p&gt;nn&lt;p&gt;The Debate is hosted by the Commission on Presidential Debates and will be moderated by NBC’s Lester Holt. Hillary Clinton will &lt;a href="https://twitter.com/CBSNews/status/780506360257675264"&gt;receive the first question&lt;/a&gt;.&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Where to Watch&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;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:&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Television:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;The event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.&lt;/p&gt;nn&lt;p&gt;&lt;strong&gt;Live Streams:&lt;/strong&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.c-span.org/2016presidentialDebates/"&gt;C-SPAN&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=L6QElWIKfDk"&gt;Washington Post (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=EuHuzhzb1nc"&gt;PBS (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=855Am6ovK7s"&gt;NBC (YouTube)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://twitter.com/i/live/778347749217406976"&gt;Bloomberg Politics (Twitter with real-time fact checking)&lt;/a&gt;&lt;/p&gt;nn&lt;p&gt;&lt;a href="https://www.facebook.com/ABCNews/"&gt;ABC (Facebook)&lt;/a&gt;&lt;/p&gt;nn&lt;hr/&gt;nn&lt;h2&gt;&lt;strong&gt;Live Discussion&lt;/strong&gt;&lt;/h2&gt;nn&lt;p&gt;If you&amp;#39;d like to participate in real-time discussion, we encourage you to head over to &lt;a href="https://discord.gg/caAhhCv"&gt;our Discord server&lt;/a&gt; and check out our &lt;a href="https://discordapp.com/channels/154635757615710208/229738984618721280"&gt;Presidential Debate Channel&lt;/a&gt;. &lt;/p&gt;nn&lt;hr/&gt;nn&lt;p&gt;Please use this thread to discuss your tonight&amp;#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. &lt;strong&gt;This thread will be closely watched, and any rule violations are certain to be met with heavy-handed moderation. Be nice!&lt;/strong&gt;&lt;/p&gt;n&lt;/div&gt;&lt;!-- SC_ON --&gt;",
"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.nnThe 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).nn-----nn**Where to Watch**n-nnUnlike 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:nn**Television:**nnThe event will be broadcast on most major TV channels, including ABC, CBS, CNN, C-SPAN, Fox News, MSNBC, and Univision.nn**Live Streams:**nn[C-SPAN](https://www.c-span.org/2016presidentialDebates/)nn[Washington Post (YouTube)](https://www.youtube.com/watch?v=L6QElWIKfDk)nn[PBS (YouTube)](https://www.youtube.com/watch?v=EuHuzhzb1nc)nn[NBC (YouTube)](https://www.youtube.com/watch?v=855Am6ovK7s)nn[Bloomberg Politics (Twitter with real-time fact checking)](https://twitter.com/i/live/778347749217406976)nn[ABC (Facebook)](https://www.facebook.com/ABCNews/)nn------nn**Live Discussion**n-nnIf 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). nn----------nnPlease 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": [{
..


but I get an error:




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)




I don't find where the issue is. Also I don't just want to print these results I get, but rather save them and use. I am not very much experienced in Java. Any help would be highly appreciated. Maybe I haven't imported any dependencies?







java json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 5:57









Busy Bee

9371619




9371619










asked Nov 11 at 16:42









Gentsview

4118




4118












  • The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
    – JB Nizet
    Nov 11 at 16:44












  • @JBNizet but there is such file in my disk. That's the issue!
    – Gentsview
    Nov 11 at 16:45








  • 1




    Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
    – JB Nizet
    Nov 11 at 16:46






  • 1




    To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
    – Thomas Kläger
    Nov 11 at 16:47










  • @JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
    – Gentsview
    Nov 11 at 16:49


















  • The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
    – JB Nizet
    Nov 11 at 16:44












  • @JBNizet but there is such file in my disk. That's the issue!
    – Gentsview
    Nov 11 at 16:45








  • 1




    Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
    – JB Nizet
    Nov 11 at 16:46






  • 1




    To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
    – Thomas Kläger
    Nov 11 at 16:47










  • @JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
    – Gentsview
    Nov 11 at 16:49
















The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
– JB Nizet
Nov 11 at 16:44






The issue is just as the error message says. There is no such file on your disk. The file is a relative one, so it's searched from the directory where you execute the java command used to start your program.
– JB Nizet
Nov 11 at 16:44














@JBNizet but there is such file in my disk. That's the issue!
– Gentsview
Nov 11 at 16:45






@JBNizet but there is such file in my disk. That's the issue!
– Gentsview
Nov 11 at 16:45






1




1




Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
– JB Nizet
Nov 11 at 16:46




Have you read my comment until the end? What's the absolute path of the directory from which you execute the java command? Whats the absolute path of the JSON file?
– JB Nizet
Nov 11 at 16:46




1




1




To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
– Thomas Kläger
Nov 11 at 16:47




To see which file Java tries to read you can add System.out.println(new File("data/reddit/redditdump.json").getAbsolutePath()); just before you try to open the file
– Thomas Kläger
Nov 11 at 16:47












@JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
– Gentsview
Nov 11 at 16:49




@JBNizet I tried writing the absolute path of the directory, didn't work /Users/myname/Desktop/javajson/src/main/resources/data/reddit/redditdump.json
– Gentsview
Nov 11 at 16:49












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Can you please try the below code.



ClassLoader classLoader = Example1.class.getClassLoader();
File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());

Object obj = parser.parse(new FileReader(file));





share|improve this answer























  • I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
    – Gentsview
    Nov 11 at 17:13










  • @Gentsview then use JSON1.class instead of getClass()
    – f1sh
    Nov 11 at 17:20










  • @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
    – Gentsview
    Nov 11 at 17:32










  • I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
    – Gentsview
    Nov 11 at 17:46










  • You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
    – f1sh
    Nov 11 at 18:17


















up vote
0
down vote













getResourceAsStream means that you have InputStream as returned object. And your exception is no suitable method found for parse(java.io.InputStream) means that method parser.parse() does not accept InputStream. You have to look at this method implementation and find out what type of objects accept this method and convert InputStream into one of it. I saw that at least String and Reader are acceptable and I convert InputStream to Reader:



public static String readReddit() {
JSONParser parser = new JSONParser();

try (Reader reader = new InputStreamReader(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"))) {
((JSONArray)parser.parse(reader)).forEach((Consumer<JSONObject>)obj -> {
String kind = (String)obj.get("kind");
JSONObject data = (JSONObject)obj.get("data");
JSONArray children = (JSONArray)data.get("children");
System.out.println("Kind: " + kind + ", total children: " + children.size());
});
} catch(Exception e) {
e.printStackTrace();
}

return null;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250905%2fno-suitable-method-found-for-parsejava-io-inputstream%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Can you please try the below code.



    ClassLoader classLoader = Example1.class.getClassLoader();
    File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());

    Object obj = parser.parse(new FileReader(file));





    share|improve this answer























    • I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
      – Gentsview
      Nov 11 at 17:13










    • @Gentsview then use JSON1.class instead of getClass()
      – f1sh
      Nov 11 at 17:20










    • @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
      – Gentsview
      Nov 11 at 17:32










    • I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
      – Gentsview
      Nov 11 at 17:46










    • You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
      – f1sh
      Nov 11 at 18:17















    up vote
    0
    down vote













    Can you please try the below code.



    ClassLoader classLoader = Example1.class.getClassLoader();
    File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());

    Object obj = parser.parse(new FileReader(file));





    share|improve this answer























    • I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
      – Gentsview
      Nov 11 at 17:13










    • @Gentsview then use JSON1.class instead of getClass()
      – f1sh
      Nov 11 at 17:20










    • @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
      – Gentsview
      Nov 11 at 17:32










    • I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
      – Gentsview
      Nov 11 at 17:46










    • You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
      – f1sh
      Nov 11 at 18:17













    up vote
    0
    down vote










    up vote
    0
    down vote









    Can you please try the below code.



    ClassLoader classLoader = Example1.class.getClassLoader();
    File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());

    Object obj = parser.parse(new FileReader(file));





    share|improve this answer














    Can you please try the below code.



    ClassLoader classLoader = Example1.class.getClassLoader();
    File file = new File(classLoader.getResource("data/reddit/redditdump.json").getFile());

    Object obj = parser.parse(new FileReader(file));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 4:30

























    answered Nov 11 at 17:05









    Manjunath H M

    10019




    10019












    • I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
      – Gentsview
      Nov 11 at 17:13










    • @Gentsview then use JSON1.class instead of getClass()
      – f1sh
      Nov 11 at 17:20










    • @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
      – Gentsview
      Nov 11 at 17:32










    • I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
      – Gentsview
      Nov 11 at 17:46










    • You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
      – f1sh
      Nov 11 at 18:17


















    • I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
      – Gentsview
      Nov 11 at 17:13










    • @Gentsview then use JSON1.class instead of getClass()
      – f1sh
      Nov 11 at 17:20










    • @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
      – Gentsview
      Nov 11 at 17:32










    • I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
      – Gentsview
      Nov 11 at 17:46










    • You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
      – f1sh
      Nov 11 at 18:17
















    I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
    – Gentsview
    Nov 11 at 17:13




    I get this error now : Error:(132, 39) java: non-static method getClass() cannot be referenced from a static context
    – Gentsview
    Nov 11 at 17:13












    @Gentsview then use JSON1.class instead of getClass()
    – f1sh
    Nov 11 at 17:20




    @Gentsview then use JSON1.class instead of getClass()
    – f1sh
    Nov 11 at 17:20












    @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
    – Gentsview
    Nov 11 at 17:32




    @f1sh tried that already and I get Error: cannot be converted to java.lang.ClassLoader
    – Gentsview
    Nov 11 at 17:32












    I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
    – Gentsview
    Nov 11 at 17:46




    I changed it to Class<JSON1> classLoader = JSON1.class; it worked but I get another exception now Class<JSON1> classLoader = JSON1.class;
    – Gentsview
    Nov 11 at 17:46












    You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
    – f1sh
    Nov 11 at 18:17




    You need access to the classloader. Use ClassLoader classLoader = JSON1.class.getClassLoader();.
    – f1sh
    Nov 11 at 18:17












    up vote
    0
    down vote













    getResourceAsStream means that you have InputStream as returned object. And your exception is no suitable method found for parse(java.io.InputStream) means that method parser.parse() does not accept InputStream. You have to look at this method implementation and find out what type of objects accept this method and convert InputStream into one of it. I saw that at least String and Reader are acceptable and I convert InputStream to Reader:



    public static String readReddit() {
    JSONParser parser = new JSONParser();

    try (Reader reader = new InputStreamReader(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"))) {
    ((JSONArray)parser.parse(reader)).forEach((Consumer<JSONObject>)obj -> {
    String kind = (String)obj.get("kind");
    JSONObject data = (JSONObject)obj.get("data");
    JSONArray children = (JSONArray)data.get("children");
    System.out.println("Kind: " + kind + ", total children: " + children.size());
    });
    } catch(Exception e) {
    e.printStackTrace();
    }

    return null;
    }





    share|improve this answer



























      up vote
      0
      down vote













      getResourceAsStream means that you have InputStream as returned object. And your exception is no suitable method found for parse(java.io.InputStream) means that method parser.parse() does not accept InputStream. You have to look at this method implementation and find out what type of objects accept this method and convert InputStream into one of it. I saw that at least String and Reader are acceptable and I convert InputStream to Reader:



      public static String readReddit() {
      JSONParser parser = new JSONParser();

      try (Reader reader = new InputStreamReader(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"))) {
      ((JSONArray)parser.parse(reader)).forEach((Consumer<JSONObject>)obj -> {
      String kind = (String)obj.get("kind");
      JSONObject data = (JSONObject)obj.get("data");
      JSONArray children = (JSONArray)data.get("children");
      System.out.println("Kind: " + kind + ", total children: " + children.size());
      });
      } catch(Exception e) {
      e.printStackTrace();
      }

      return null;
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        getResourceAsStream means that you have InputStream as returned object. And your exception is no suitable method found for parse(java.io.InputStream) means that method parser.parse() does not accept InputStream. You have to look at this method implementation and find out what type of objects accept this method and convert InputStream into one of it. I saw that at least String and Reader are acceptable and I convert InputStream to Reader:



        public static String readReddit() {
        JSONParser parser = new JSONParser();

        try (Reader reader = new InputStreamReader(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"))) {
        ((JSONArray)parser.parse(reader)).forEach((Consumer<JSONObject>)obj -> {
        String kind = (String)obj.get("kind");
        JSONObject data = (JSONObject)obj.get("data");
        JSONArray children = (JSONArray)data.get("children");
        System.out.println("Kind: " + kind + ", total children: " + children.size());
        });
        } catch(Exception e) {
        e.printStackTrace();
        }

        return null;
        }





        share|improve this answer














        getResourceAsStream means that you have InputStream as returned object. And your exception is no suitable method found for parse(java.io.InputStream) means that method parser.parse() does not accept InputStream. You have to look at this method implementation and find out what type of objects accept this method and convert InputStream into one of it. I saw that at least String and Reader are acceptable and I convert InputStream to Reader:



        public static String readReddit() {
        JSONParser parser = new JSONParser();

        try (Reader reader = new InputStreamReader(Example1.class.getClassLoader().getResourceAsStream("data/reddit/redditdump.json"))) {
        ((JSONArray)parser.parse(reader)).forEach((Consumer<JSONObject>)obj -> {
        String kind = (String)obj.get("kind");
        JSONObject data = (JSONObject)obj.get("data");
        JSONArray children = (JSONArray)data.get("children");
        System.out.println("Kind: " + kind + ", total children: " + children.size());
        });
        } catch(Exception e) {
        e.printStackTrace();
        }

        return null;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 5:11

























        answered Nov 12 at 4:53









        oleg.cherednik

        4,7032916




        4,7032916






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250905%2fno-suitable-method-found-for-parsejava-io-inputstream%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python