0

I want to know if it is possible to check if some key exists in some jsonArray using java. For example: lets say that I have this json string:

{'abc':'hello','xyz':[{'name':'Moses'}]}

let's assume that this array is stored in jsnArray from Type JSONArray. I want to check if 'abc' key exists in the jsnArray, if it exists I should get true else I should get false (in the case of 'abc' I should get true). Thnkas

2
  • 3
    This is not a valid json array. Either you mean {'abc':'hello','xyz':[{'name':'Moses'}]} or [{'abc':'hello'}, {'xyz':[{'name':'Moses'}}]? Commented Mar 15, 2013 at 17:20
  • I will change it, thanks. still how I do what I've asked? Commented Mar 15, 2013 at 17:22

4 Answers 4

2

What you posted is a JSONObject, inside which there is a JSONArray. The only array you have in this example is the array 'xyz', that contains only one element.

A JSONArray example is the following one:

{
 'jArray':
          [
           {'hello':'world'},
           {'name':'Moses'},
           ...
           {'thisIs':'theLast'}
          ]
}

You can test if a JSONArray called jArray, included inside a given JSONObject (a situation similar to the example above) contains the key 'hello' with the following function:

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;
}

And calling that in this way:

containsKey(myJsonObject, "hello");
Sign up to request clarification or add additional context in comments.

1 Comment

i think it soulde be with try/ catch
1

Using regular expressions will not work because of the opening and closing brackets.

You could use a JSON library (like google-gson) to transform your JSON Array into a java array and then handle it.

Comments

1

JSON arrays don't have key value pairs, JSON objects do.

If you store it as a json object you can check the keys using this method: http://www.json.org/javadoc/org/json/JSONObject.html#has(java.lang.String)

4 Comments

is there any why to convert jsonarray to json object?
Is there a reason you can't simply parse the input as a JSONObject in the first place? From your syntax above, it looks like you're receiving a JSONObject anyway, so you shouldn't be able to save it as a JSONArray in the first place.
I am using Parse as my cloude database. and I want to store this "complex" json array in one of the fields. Maybe it is better to store the json as a string in the Parse db?
I don't know much about parse sorry, but yes a string should also work. It seems to me if parse is capable of returning a json array it should be capable of returning a json object? Alternatively you could wrap your object in an array, like: [{'abc':'hello','xyz':[{'name':'Moses'}]}] then get the first item in that array as a jsonObject: JSONObject myJSONObject = myJSONArray.get(0)
1

If you use JSON Smart Library in Java to parse JSon String -

You can parse JSon Array with following code snippet -

like -

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..
}

You can have more information in - https://code.google.com/p/json-smart/

Hope this help you...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.