Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Accessing deeper levels of JSON object

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing deeper levels of JSON object

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing deeper levels of JSON object

    Is your problem with using the java language
    or is it about how to use the classes and methods in the org.json.simple package?
    If the problems are with the import org.json.simple package, then you need to ask your questions on a site that supports that package.

    If the problems are with java, copy any relevant output and paste it here with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. large JSON object with multithreading
    By JulieCalm in forum Threads
    Replies: 0
    Last Post: May 1st, 2019, 11:46 AM
  2. JSON Object parsing
    By shama786 in forum Member Introductions
    Replies: 2
    Last Post: June 26th, 2017, 09:13 AM
  3. Plot a chart using Jqplot for a JSON object
    By adhithyan90 in forum Other Programming Languages
    Replies: 6
    Last Post: June 5th, 2014, 10:44 AM
  4. Exception while reading JSON object
    By swarna in forum Java Theory & Questions
    Replies: 4
    Last Post: February 21st, 2013, 12:33 AM
  5. Exception while reading JSON object
    By swarna in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 20th, 2013, 03:21 PM