I have been trying to figure this out for a few days. I have a background in programming in PHP and other languages. But I haven't programmed in a long time and trying to relearn as well as get into Java. I figured out (after some work) how to pull data from a URL (JSON) and then turn it into an object. I downloaded simpleJSON and fiddled with a lot of different code before I put something together that works on a basic level.
package findscripturemain; // Required imports import java.util.Scanner; import java.net.*; import java.io.*; import org.json.simple.*; import org.json.simple.parser.*; public class findscripture { public static void main(String args[]) { // Basic information System.out.println("findscripture.io"); System.out.println("By: Ninjakreborn"); // Open scanning Scanner scan = new Scanner(System.in); // Basic variables String book_name; int chapter; int verse; // Gather book name, chapter and verse System.out.println("What is the book name?"); book_name = scan.next(); System.out.println("What chapter?"); chapter = scan.nextInt(); System.out.println("What verse?"); verse = scan.nextInt(); // Print back what they have entered. System.out.println("You have selected " + book_name + " Chapter " + chapter + " verse " + verse); // Grab verse text and display it String output = getUrlContents("###"); System.out.println(output); JSONParser parser = new JSONParser(); JSONObject obj; //JSONArray array; try { obj = (JSONObject)parser.parse(output); //array = (JSONArray)parser.parse(output); System.out.println(obj.get("data")); System.out.println(obj.get("orgId")); System.out.println(obj.get("bookId")); } catch(ParseException e) { e.printStackTrace(); } System.out.println(output); // Close scanner scan.close(); } // Get the contents of a URL private static String getUrlContents(String theUrl) { StringBuilder content = new StringBuilder(); // Use try and catch to avoid the exceptions try { URL url = new URL(theUrl); // creating a url object URLConnection urlConnection = url.openConnection(); // creating a urlconnection object urlConnection.setRequestProperty("api-key", "a0e5c440e7b198ddf9dfbc4fb3f42d99"); // wrapping the urlconnection in a bufferedreader BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; // reading from the urlconnection using the bufferedreader while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); } bufferedReader.close(); } catch(Exception e) { e.printStackTrace(); } return content.toString(); } }
That is the code that I put together after a lot of trial and error and learning. I wanted to get it into an array so it was easier to work with but I couldn't figure out how to make all that work. The output of the JSON is below.
{"data":{"id":"JHN.3.16","orgId":"JHN.3.16","bookI d":"JHN","chapterId":"JHN.3","bibleId":"de4e12af7f 28f599-01","reference":"John 3:16","content":"<p class=\"p\"><span data-number=\"16\" data-sid=\"JHN 3:16\" class=\"v\">16</span><span class=\"wj\">ΒΆ For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.</span> </p>","verseCount":1,"copyright":"\n \n PUBLIC DOMAIN except in the United Kingdom, where a Crown Copyright applies to printing the KJV. \n ","next":{"id":"JHN.3.17","number":"17"},"previous ":{"id":"JHN.3.15","number":"15"}},"meta":{"fums": "<script>\nvar _BAPI=_BAPI||{};\nif(typeof(_BAPI.t)==='undefined' ){\ndocument.write('\\x3Cscript src=\"'+document.location.protocol+'//cdn.scripture.api.bible/fums/fumsv2.min.js\"\\x3E\\x3C/script\\x3E');}\ndocument.write(\"\\x3Cscript\\x3E _BAPI.t('129becd7-df82-4052-b2db-5da3fec3291f');\\x3C/script\\x3E\");\n</script><noscript><img src=\"https://d3a2okcloueqyx.cloudfront.net/nf1?t='129becd7-df82-4052-b2db-5da3fec3291f'\" height=\"1\" width=\"1\" border=\"0\" alt=\"\" style=\"height: 0; width: 0;\" /></noscript>","fumsId":"129becd7-df82-4052-b2db-5da3fec3291f","fumsJsInclude":"cdn.scripture.api.b ible/fums/fumsv2.min.js","fumsJs":"var _BAPI=_BAPI||{};if(typeof(_BAPI.t)!='undefined'){ _BAPI.t('129becd7-df82-4052-b2db-5da3fec3291f'); }","fumsNoScript":"<img src=\"https://d3btgtzu3ctdwx.cloudfront.net/nf1?t=129becd7-df82-4052-b2db-5da3fec3291f\" height=\"1\" width=\"1\" border=\"0\" alt=\"\" style=\"height: 0; width: 0;\" />"}}
So the issue is I don't know how to access variables underneath data. So data is the upper level but I don't know how to access id, orgId, book Id, and all this other data throughout the object that it has created.
Any feedback is appreciated. Also any other tips and random advice on java in general will help. Because I'm trying to learn it.