Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
3  
This is not a valid json array. Either you mean {'abc':'hello','xyz':[{'name':'Moses'}]} or [{'abc':'hello'}, {'xyz':[{'name':'Moses'}}]? – Alex Mar 15 at 17:20
I will change it, thanks. still how I do what I've asked? – Vlad Ioffe Mar 15 at 17:22

4 Answers

up vote 1 down vote accepted

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");
share|improve this answer
i think it soulde be with try/ catch – Vlad Ioffe Mar 15 at 17:58
Done. Thank you very much :) – VitoShadow Mar 15 at 18:07

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.

share|improve this answer

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)

share|improve this answer
is there any why to convert jsonarray to json object? – Vlad Ioffe Mar 15 at 17:27
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. – not_john Mar 15 at 17:30
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? – Vlad Ioffe Mar 15 at 17:32
1  
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) – not_john Mar 15 at 17:41

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.