2012-07-13 115 views
0

我在安卓所以請幫助我初學者,我想提取XML解析字符串JSON數組是下面:JSON提取從XML

<?xml version="1.0" encoding="UTF-8"?> 
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> 
[{"id":3033,"type":"cpt4","description":"New Patient Office Visit for Moderate to Severe problems- Requiring Longer Visit","category":"Office Visits","subCategory":"Illness Related Visits","inNetwork":195.99,"outNetwork":424.06,"code":"99205","zip":"90210","lastupdated":"2012-02-13T00:00:00.000-05:00"}] 
</string> 

如何,我們可以從上面的字符串中的Android得JSON字符串,幫我。

+0

這[線程] [1]可能已經有你的答案。 [1]:http://stackoverflow.com/questions/814001/json-net-convert-json-string-to-xml-or-xml-to-json-string – rshahriar 2012-07-13 09:46:55

+1

[Extract json array in Android的XML](http://stackoverflow.com/questions/11465787/extract-json-array-in-android-from-xml) - 請不要轉發問題。 – 2012-07-13 10:08:06

回答

1

這裏是GSON

  • 下載解決方案GSON從上面的鏈接 jar添加其添加到您的Buildpath

在這裏,我試圖解析XML得到JSON字符串,然後解析json以獲得必要的元素

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
     + "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">" 
     + "[{\"id\":3033,\"type\":\"cpt4\",\"description\":\"New Patient Office Visit for Moderate to Severe problems- " 
     + "Requiring Longer Visit\",\"category\":\"Office Visits\",\"subCategory\":\"Illness Related Visits\"," 
     + "\"inNetwork\":195.99,\"outNetwork\":424.06,\"code\":\"99205\",\"zip\":\"90210\",\"lastupdated\":\"2012-02-13T00:00:00.000-05:00\"}]" 
     + "</string>"; 

Document doc = null; 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
try { 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xml)); 
    doc = db.parse(is); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

doc.getDocumentElement().normalize(); 
String text = doc.getDocumentElement().getTextContent(); 
System.out.println(text); 

try { 
    // testing whether the json string is valid or not 
    JsonParser jsonParser = new JsonParser(); 
    JsonElement jsonElement = jsonParser.parse(text); 
    System.out.println("Valid JSon"); 

    System.out.println("isArray : " + jsonElement.isJsonArray()); 

    JsonArray jsonArray = jsonElement.getAsJsonArray(); 
    JsonObject jsonObject = jsonArray.get(0).getAsJsonObject(); 
    System.out.println("id : " + jsonObject.get("id")); 
    System.out.println("type : " + jsonObject.get("type")); 
    System.out 
      .println("description : " + jsonObject.get("description")); 
    System.out.println("category : " + jsonObject.get("category")); 
    System.out 
      .println("subCategory : " + jsonObject.get("subCategory")); 
    System.out.println("inNetwork : " + jsonObject.get("inNetwork")); 
    System.out.println("outNetwork : " + jsonObject.get("outNetwork")); 
    System.out.println("code : " + jsonObject.get("code")); 
    System.out.println("zip : " + jsonObject.get("zip")); 
    System.out 
      .println("lastupdated : " + jsonObject.get("lastupdated")); 

} catch (JsonSyntaxException e2) { 
    // TODO Auto-generated catch block 
    e2.printStackTrace(); 
}  

注: - (當然,你可以嘗試沒有GSON和使用本機JSON解析器)

+0

你能測試這個嗎? – sunil 2012-07-16 07:46:44