JavaJSONArray和动态键

JavaJSONArray和动态键,java,json,Java,Json,我有一个JSON的这种格式的动态键的电子邮件 { "id":johnsmith@gmail.com", "contact":[ { "uid":0, "name":"johnsmith", "email":[ { "home":"johnsmith0@gmail.com" }, { "work":"johnsmith1@gmail.com" }

我有一个JSON的这种格式的动态键的电子邮件

{
   "id":johnsmith@gmail.com",
   "contact":[
  {
     "uid":0,
     "name":"johnsmith",
     "email":[
        {
           "home":"johnsmith0@gmail.com"
        },
        {
           "work":"johnsmith1@gmail.com"
        }
     ],
     "mobile":[
        {
           "cc":"+60",
           "mobile":"00000000"
        },
        {
           "cc":"+60",
           "mobile":"00000001"
        }
     ]
  }
   ]
}
我试过了

Iterator it = contactArray.getJSONObject(i)
            .getJSONObject("email").keys();
但我有个错误

org.json.JSONException: JSONObject["email"] is not a JSONObject.
但是这样做是可行的,但是没有从JSONArray获取密钥的方法

            JSONArray emailArray = contactArray.getJSONObject(i)
                    .getJSONArray("email");

如何处理动态关键点?谢谢。

数组没有键,它们有元素。这应基于java集合类型,使用循环结构对其进行测试:

JSONArray emailArray = contactArray.getJSONObject(i).getJSONArray("email");

for(Object o: emailArray){
    System.out.println(o);
}
如果emailArray为空,则不会输出任何内容,如果其中包含元素,则会输出值