[英]Parse List from Json
我有JSON文件內容,如下所示:
{"name":"abg","phone":["12313","4654654","4546"],"city":"NY"}
{"name":"jea","phone":["8941","978","`13421","45231"],"city":"LA"}
為了解析文件,我使用了以下代碼:
String var1 = output;
JSONObject obj;
try {
obj = new JSONObject(var1);
String a = obj.getString("name");
String b = obj.getString("phone");
String c = obj.getString("city");
System.out.println("name:" + a);
System.out.println("phone:" + b);
System.out.println("city:" + c);}
我該如何解析電話?
您可以使用如下所示的內容:
obj = new JSONObject(var1);
JSONArray jsonArr = obj.getJSONArray("phone");
for(int i=0; i<jsonArr.length();i++) {
System.out.println(jsonArr.get(i));
}
您可以使用org.json包的JSONObject和JSONArray類,如下所示:
JSONObject obj = new JSONObject("{\"name\":\"abg\",\"phone\":[\"12313\",\"4654654\",\"4546\"],\"city\":\"NY\"}");
JSONArray array = obj.getJSONArray("phone");
for(int i=0; i<array.length();i++) {
System.out.println(array.get(i));
}
obj = new JSONObject(var1);
//other objects
JsonArray jsonArr = obj.getJsonArray("phone");
for(obj:jsonArr){
System.out.println("phone:" + obj);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.