java嵌套JSONArray

java嵌套JSONArray,java,json,parsing,arrays,Java,Json,Parsing,Arrays,我想知道是否可以使用java检查某个jsonArray中是否存在某个键。例如:假设我有这个json字符串: {'abc':'hello','xyz':[{'name':'Moses'}]} 让我们假设这个数组是从类型JSONArray存储在jsnArray中的。 我想检查jsnArray中是否存在“abc”键,如果它存在,我应该得到true,否则我应该得到false(如果是“abc”,我应该得到true)。 Thnkas由于左括号和右括号的缘故,使用正则表达式将不起作用 您可以使用JSON库(

我想知道是否可以使用java检查某个jsonArray中是否存在某个键。例如:假设我有这个json字符串:

{'abc':'hello','xyz':[{'name':'Moses'}]}
让我们假设这个数组是从类型
JSONArray
存储在jsnArray中的。 我想检查jsnArray中是否存在“abc”键,如果它存在,我应该得到
true
,否则我应该得到
false
(如果是“abc”,我应该得到true)。
Thnkas

由于左括号和右括号的缘故,使用正则表达式将不起作用


您可以使用JSON库(如)将JSON数组转换为java数组,然后对其进行处理。

使用正则表达式将不起作用,因为有左括号和右括号


您可以使用JSON库(如)将JSON数组转换为java数组,然后处理它。

JSON数组没有键值对,JSON对象有

如果将其存储为json对象,则可以使用以下方法检查密钥:
JSON数组没有键值对,JSON对象有键值对

如果将其存储为json对象,则可以使用以下方法检查密钥:

如果您使用Java中的JSON智能库解析JSON字符串-

您可以使用以下代码段解析JSon数组-

像-

JSONObject resultsJSONObject = (JSONObject) JSONValue.parse(<<Fetched JSon String>>);
JSONArray dataJSon = (JSONArray) resultsJSONObject.get("data");
JSONObject[] updates = dataJSon.toArray(new JSONObject[dataJSon.size()]);

for (JSONObject update : updates) {
            String message_id = (String) update.get("message_id");
            Integer author_id = (Integer) update.get("author_id");
            Integer createdTime = (Integer) update.get("created_time");
            //Do your own processing...
            //Here you can check null value or not..
}
JSONObject resultsJSONObject=(JSONObject)JSONValue.parse();
JSONArray dataJSon=(JSONArray)resultsJSONObject.get(“数据”);
JSONObject[]updates=dataJSon.toArray(新的JSONObject[dataJSon.size()]);
用于(JSONObject更新:更新){
String message_id=(String)update.get(“message_id”);
整数author\u id=(整数)update.get(“author\u id”);
Integer createdTime=(Integer)update.get(“created_time”);
//做你自己的处理。。。
//在这里,您可以检查空值或不检查空值。。
}
你可以在以下网址获得更多信息:


如果您使用Java中的JSON智能库解析JSON字符串,希望这能帮助您…

-

您可以使用以下代码段解析JSon数组-

像-

JSONObject resultsJSONObject = (JSONObject) JSONValue.parse(<<Fetched JSon String>>);
JSONArray dataJSon = (JSONArray) resultsJSONObject.get("data");
JSONObject[] updates = dataJSon.toArray(new JSONObject[dataJSon.size()]);

for (JSONObject update : updates) {
            String message_id = (String) update.get("message_id");
            Integer author_id = (Integer) update.get("author_id");
            Integer createdTime = (Integer) update.get("created_time");
            //Do your own processing...
            //Here you can check null value or not..
}
JSONObject resultsJSONObject=(JSONObject)JSONValue.parse();
JSONArray dataJSon=(JSONArray)resultsJSONObject.get(“数据”);
JSONObject[]updates=dataJSon.toArray(新的JSONObject[dataJSon.size()]);
用于(JSONObject更新:更新){
String message_id=(String)update.get(“message_id”);
整数author\u id=(整数)update.get(“author\u id”);
Integer createdTime=(Integer)update.get(“created_time”);
//做你自己的处理。。。
//在这里,您可以检查空值或不检查空值。。
}
你可以在以下网址获得更多信息:


希望这能帮助你…

你发布的是一个
JSONObject
,里面有一个
JSONArray
。本例中唯一的数组是只包含一个元素的数组
'xyz'

JSONArray示例如下所示:

{
 'jArray':
          [
           {'hello':'world'},
           {'name':'Moses'},
           ...
           {'thisIs':'theLast'}
          ]
}
您可以测试一个名为
jArray
JSONArray
,包含在给定的
JSONObject
中(类似于上述示例的情况)是否包含具有以下函数的键“hello”:

boolean containsKey(JSONObject myJsonObject, String key) {
    boolean containsHelloKey = false;
    try {
        JSONArray arr = myJsonObject.getJSONArray("jArray");
        for(int i=0; i<arr.length(); ++i) {
            if(arr.getJSONObject(i).get(key) != null) {
               containsHelloKey = true;
               break;
            }
        }
    } catch (JSONException e) {}

    return containsHelloKey;
}

你发布的是一个
JSONObject
,里面有一个
JSONArray
。本例中唯一的数组是只包含一个元素的数组
'xyz'

JSONArray示例如下所示:

{
 'jArray':
          [
           {'hello':'world'},
           {'name':'Moses'},
           ...
           {'thisIs':'theLast'}
          ]
}
您可以测试一个名为
jArray
JSONArray
,包含在给定的
JSONObject
中(类似于上述示例的情况)是否包含具有以下函数的键“hello”:

boolean containsKey(JSONObject myJsonObject, String key) {
    boolean containsHelloKey = false;
    try {
        JSONArray arr = myJsonObject.getJSONArray("jArray");
        for(int i=0; i<arr.length(); ++i) {
            if(arr.getJSONObject(i).get(key) != null) {
               containsHelloKey = true;
               break;
            }
        }
    } catch (JSONException e) {}

    return containsHelloKey;
}

这不是有效的json数组。你的意思是
{'abc':'hello','xyz':[{'name':'Moses'}}
或者
{'abc':'hello'},{'xyz':[{'name':'Moses'}}]
?我会更改它的,谢谢。我仍然是怎么做的?这不是一个有效的json数组。你的意思是
{'abc':'hello','xyz:[{'name':'Moses'}}}
或者
{'abc':'xyz
?我会更改它,谢谢。我还是要怎么做?有什么理由将jsonarray转换为json对象吗?有什么理由不能首先将输入解析为JSONObject吗?从上面的语法来看,看起来您正在接收JSONObject,所以您不应该在第一个位置将其保存为jsonarraye、 我正在使用Parse作为我的cloude数据库。我想存储这个“复合体”json数组位于其中一个字段中。也许最好将json作为字符串存储在Parse db中?对不起,我对Parse不太了解,但字符串也应该起作用。在我看来,如果Parse能够返回json数组,它应该能够返回json对象?或者,您可以将对象包装在数组中,如:[{'abc':'hello','xyz':[{'name':'Moses'}]}]然后将该数组中的第一项作为jsonObject获取:jsonObject myJSONObject=myJSONArray.get(0)有什么理由将jsonarray转换为json对象吗?有什么原因不能首先将输入解析为JSONObject吗?从上面的语法来看,您似乎正在接收JSONObject,因此您首先不能将其保存为jsonarray。我使用parse作为我的cloude数据库。我想存储它中国的“情结”json数组位于其中一个字段中。也许最好将json作为字符串存储在Parse db中?对不起,我对Parse不太了解,但字符串也应该起作用。在我看来,如果Parse能够返回json数组,它应该能够返回json对象?或者,您可以将对象包装在数组中,如:[{'abc':'hello','xyz':[{'name':'Moses'}]}]然后将该数组中的第一项作为jsonObject获取:jsonObject myJSONObject=myJSONArray.get(0)我认为应该与try/catchDone一起使用。非常感谢:)我认为应该与try/catchDone一起使用。非常感谢:)