Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use getJSONObject(int)

Tags:

java

json

android

I'm trying to parse some JSON. Here is the code. frc-manual.usfirst.org/a/GetAllItems/ManualID=3 I have been trying for several hours to get it work but every example I have seen online uses getJSONObject(int) but I can only use getJSONObject(String). This is making impossible. Am I overlooking something?

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.annotation.SuppressLint;

public class JSON {
   private String html = "html";
   private String version = "version";
   private String pageString = null;
   private String urlString = "http://frc-manual.usfirst.org/a/GetAllItems/ManualID=3";

   public volatile boolean parsingComplete = true;
   public JSON(String page){
      this.pageString = page;
   }
   public String getHTML(){
      return html;
   }
   public String getVersion(){
      return version;
   }

   @SuppressLint("NewApi")
   public void readAndParseJSON(String in) {
      try {
         JSONObject reader = new JSONObject(in);

         JSONObject head = reader.getJSONObject("data").getJSONObject("SubChapter").getJSONObject("3").getJSONObject("children").getJSONObject(pageString);
         if(head != null){
             html = head.getString("item_content_text");
             html = html + head.length();
             for(int i = 0; i < head.length();i++){
                 JSONObject children = head.getJSONObject(i);
                 if(children != null){
                     html = html + children.getString("item_content_text");
                 }

             } 
         }

         //html = html + listFromJsonSorted(head.getJSONObject("children"));





         JSONObject main  = reader.getJSONObject("data");
         version = main.getString("LatestManualUpdate");

         parsingComplete = false;



        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

   }
   public void fetchJSON(){
      Thread thread = new Thread(new Runnable(){
         @Override
         public void run() {
         try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
         InputStream stream = conn.getInputStream();

      String data = convertStreamToString(stream);

      readAndParseJSON(data);
         stream.close();

         } catch (Exception e) {
            e.printStackTrace();
         }
         }
      });

       thread.start();      
   }
   static String convertStreamToString(java.io.InputStream is) {
      java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
      return s.hasNext() ? s.next() : "";
   }

}
like image 544
AquaMorph Avatar asked Aug 12 '26 03:08

AquaMorph


1 Answers

Probably what you are seeing in this example is a JSONArray.

JSONArray arr = json.getJSONArray("array");
JSONObject obj = arr.getJSONObject(0);

In your case, i don't see any array in this JSON, head in your code is a JSONObject. To get the item_content_text you just need head.getString("item_content_text");

You can do this to get all childrens in your JSON:

html = head.getString("item_content_text");
JSONObject children = head;
while (children.containsKey("children")) {
   children = children.getJSONObject("children");
   html += children.getString("item_content_text");
}
like image 143
Tiago Engel Avatar answered Aug 14 '26 16:08

Tiago Engel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!