2013-11-04 91 views
0

我是新來的JSON從JSON文件中的數據,所以請幫助我獲得解決這個如何獲得在下面的代碼

propertyAlerts: [ 
{ 
    alertDomain: "oiq.core.alert.PropertyAlert", 
    alertType: "HERITAGE_DETECTED", 
    oiqCreatedDate: "2013-11-04 03:06:26" 
}] 

,通過使用Java,我希望得到以下數據

OUTPUT: 
alertDomain: "oiq.core.alert.PropertyAlert" 
alertType: "HERITAGE_DETECTED" 
oiqCreatedDate: "2013-11-04 03:06:26" 

使用由我

public void checklicense(String filename) throws Exception 
    { 
JSONParser parser=new JSONParser(); 
Object obj = parser.parse(new FileReader("./output_profiles/"+filename)); 
JSONObject jsonObject = (JSONObject) obj; 
JSONArray jsonMainArr = obj.getJSONArray("propertyalert"); 
JSONObject childJSONObject = jsonMainArr.getJSONObject(i); 
    String alertDomain = childJSONObject.getString("alertDomain"); 

} 

任何一個可以幫助我解決這個問題,下面

回答

0

This教程解釋了JSON解析的基礎知識。

我會建議你閱讀整篇文章,因爲它幾乎每天都會在Android開發中完成。

0
public static void checklicense(String filename) 
{ 
    try { 
     JSONParser parser = new JSONParser(); 
     Object obj = parser.parse(new FileReader(filename)); 
     System.out.println(obj.getClass()); 
     JSONObject jsonObject = (JSONObject) obj; 
     JSONArray jsonMainArr = (JSONArray) jsonObject.get("propertyAlerts"); 
     Iterator iterator = jsonMainArr.iterator(); 
     while(iterator.hasNext()) { 
      jsonObject =(JSONObject) iterator.next(); 
      String alertDomain = (String) jsonObject.get("alertDomain"); 
      String alertType = (String) jsonObject.get("alertType"); 
      System.out.println("alertDomain " + alertDomain + ", alertType " + alertType); 
     } 
    } catch (Exception ex) { 
     java.util.logging.Logger.getLogger(EosClient.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

以上代碼生成所需的輸出爲一個有效的JSON輸入

{ 
"propertyAlerts": [ 
    { 
     "alertDomain": "oiq.core.alert.PropertyAlert", 
     "alertType": "HERITAGE_DETECTED", 
     "oiqCreatedDate": "2013-11-04 03:06:26" 
    } 
] 
} 
相關問題