2014-05-16 32 views
1

我想解析一些JSON。這是代碼。 frc-manual.usfirst.org/a/GetAllItems/ManualID=3我一直在嘗試幾個小時才能正常工作,但我在網上看到的每個示例都使用getJSONObject(int),但我只能使用getJSONObject(String)。這是不可能的。我可以忽略一些東西嗎使用getJSONObject(int)

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() : ""; 
    } 

} 

回答

0

您只能使用getJSONObject(String)getJSONObject(int),因爲只需要字符串的方法。檢查documentation here ..

的原因,是因爲在JSON鍵總是字符串僅.. read here

我認爲你正在尋找檢索JSON的一個int值,但你糊塗了..這樣做會getInt("key_name")如果JSON確實有int值一鍵的方式..

1

可能你在這個例子中看到的是一個JSONArray。

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

在你的情況,我沒有看到這個JSON任何陣列,在你的代碼是一個JSONObject。要獲得item_content_text你只需要head.getString("item_content_text");

你可以做到這一點,以獲得您的JSON所有兒童:當然

html = head.getString("item_content_text"); 
JSONObject children = head; 
while (children.containsKey("children")) { 
    children = children.getJSONObject("children"); 
    html += children.getString("item_content_text"); 
} 
+0

是的,但我也需要在兒童節點 – AquaMorph

+0

,但你已經得到了你的代碼中的子節點,我沒有看到更多的孩子在你發佈的JSON作爲例子 –

+0

我想我明白你的問題,你可以有一個對象,有一個孩子,也許有一個孩子,等等。所以你可以做到這一點。 JSONObject children = head.getJSONObject(「children」);如果(children!= null)concat –

0

就可以了,只是我想你的「頭」變量應該有一個返回類型JSONArray而不是JSONObject。

我在我的程序運行這段代碼 -

//response = some http response 
    final JSONObject object = new JSONObject(response); 
    final JSONArray array = object.getJSONArray("repeatedStuff"); //$NON-NLS-1$ 
    Assert.assertEquals(2, array.length()); 
    for (int i = 0; i < array.length(); i++) { 
     final JSONObject element = array.getJSONObject(i); 
     //do something 
    } 

你也可以參考這個鏈接,看看他們在做什麼 - android: The method getJSONObject(int) in the type JSONArray is not applicable for the arguments (String)

讓我知道這是否有助於!

+0

它想讓我將JSONArray更改回JSONObject – AquaMorph

+0

您是否嘗試從Object類型轉換爲(JSONArray)? – ND27

+0

要回答您的第一個問題 - 是的,如果您使用JSONArray製作JSONObjects循環。 – ND27