从Json解析列表

时间:2018-06-13 07:44:28

标签: 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;
                   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);}

我该如何解析手机?

3 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

obj = new JSONObject(var1);
JSONArray jsonArr = obj.getJSONArray("phone");

for(int i=0; i<jsonArr.length();i++) {
    System.out.println(jsonArr.get(i));
}

答案 1 :(得分:0)

您可以使用 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));
    }

答案 2 :(得分:0)

FROM ubuntu

RUN apt-get update && apt-get install -y apache2 && apt-get  install nginx  -y


#COPY resources   /var/www/html/

#VOLUME /var/www/html:/var/www/html

COPY /var/www/html/ /var/www/html/
CMD ["nginx", "-g", "daemon off;"]