Java 从Json解析列表

Java 从Json解析列表,java,arrays,json,list,parsing,Java,Arrays,Json,List,Parsing,我有JSON文件内容,具体如下: {"name":"abg","phone":["12313","4654654","4546"],"city":"NY"} {"name":"jea","phone":["8941","978","`13421","45231"],"city":"LA"} 要解析文件,我使用以下代码: String var1 = output; JSONObject obj;

我有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));
}
obj=新的JSONObject(var1);
JSONArray jsonArr=obj.getJSONArray(“电话”);

对于(int i=0;i您可以使用JSONObjectJSONArrayorg.json包的类,如下所示:

    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));
    }
JSONObject obj=新的JSONObject(“{\'name\':\'abg\',\'phone\':[\'12313\',\'4654654\',\'4546\'],\'city\':'NY\'”);
JSONArray数组=obj.getJSONArray(“电话”);

对于(int i=0;i可能与我以动态方式打印它的方式重复?system.out.println(jsonArr.getString(i));@eyal1506,您可以使用一个for循环,如Yug的答案中所述。还更新了我的答案。
obj = new JSONObject(var1);
//other objects
JsonArray jsonArr = obj.getJsonArray("phone");

for(obj:jsonArr){
  System.out.println("phone:" + obj);
}