使用java解析JSON文件中的所有JSONObject

问题描述:

我的目的是解析JSON文件中描述的twitter数据。我创建了解析JSON,其功能非常空洞,但它告诉我只有一个JSONObject的,而我的`使用java解析JSON文件中的所有JSONObject

public void parsingJson(String d) throws Exception 
     { 

    BufferedReader br = null; 
    //JSONObject rootObejct=null; 
    JSONObject js=new JSONObject(); 

    try { 

     String sCurrentLine; 
     InputStream inputStream = new FileInputStream("E://inputdata.json") ; 
     InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
     br = new BufferedReader(inputStreamReader); 

     while ((sCurrentLine = br.readLine())!= null) { 
      JSONObject rootObejct=new JSONObject(sCurrentLine); 


      for(@SuppressWarnings("unchecked") 
      Iterator<String> iter = rootObejct.keys();iter.hasNext();) { 
        String key = iter.next(); 

        try { 
         if (key.startsWith(d)){ 
         System.out.println("******key*********"+key); 
         Object value = rootObejct.get(key); 
         System.out.println("keys vlaue "+value.toString()); 
         } 
        } catch (JSONException e) { 
         // Something went wrong! 
        } 

      } 
      } 

`

这里有一个简单的代码来读取一个JSON文件

public void readJSONFile(String filePath) { 
    try { 
    JSONParser parser = new JSONParser(); 
    Object obj = parser.parse(new FileReader(filePath)); 

    JSONObject jsonObject = (JSONObject) obj; 

    //if your value is an array 
    JSONArray arr = (JSONArray) jsonObject.get("array"); 
    //if your value is string 
    String str = (String) jsonObject.get("status"); 

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

} 

JSON用作测试用例的文件是

{ 
     "status": "OK", 
     "array": [ "Hello" ] 
    } 

如果没有帮助,请告诉我。