2

I am using net.sf.json library for json To java and vice-versa conversion

i have a following string:

String jsonStr = "{\"name\" : \"abc\",\"address\"  : \"def\"}";    // line 1

I tried to verify above string as valid json array using below code :

JSONArray arr = JSONArray.fromObject(jsonStr);      // line 2
System.out.println(arr.isArray());       // line 3

But i get the following exception at line 2

Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at character 1 of {"name" : "abc","address"  : "def"}
    at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
    at net.sf.json.JSONArray._fromJSONTokener(JSONArray.java:1146)
    at net.sf.json.JSONArray._fromString(JSONArray.java:1226)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:151)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:129)
    at com.example.WsClient.main(WsClient.java:2) // 

Now there is no static method in JSONArray class to verify it as valid json string, so i have to create a json array and then verify it. But i am getting the exception while creating the array. Then how can i use this method ?

How can i verify that above string is a valid json Array ?

EDIT : Currently the string is an object but if i create a method with String as argument to convert to a jsonArray. How can i verify the string as valid json array?

2 Answers 2

2

From net.sf.json.JSONArray.fromObject() docu:

Throws: JSONException - if the object can not be converted to a proper JSONArray.

So you already validated your string via exception, and you know this is not a JSON valid array string. If you get this JSON string as method argument you could re-throw the exception (by your method), or handle locally (by returning null).

isArray() method is part of the Interface JSON which is implemented by JSONArray.

boolean isArray() Returns true if this object is a JSONArray, false otherwise.

I am expecting you can call this later if you have actually valid parsed JSON array and if your object is JSONArray.

What I am trying to say you don't need special check if this string is valid. You have two solutions:

public XXX parseYourString(String json) throws net.sf.json.JSONException {
    JSONArray arr = JSONArray.fromObject(json);
    // do something with arr and return custom result
}

or:

public XXX parseYourString(String json) {
    XXX xxx = new XXX();
    try {
          JSONArray arr = JSONArray.fromObject(json);

          // do something with arr

    } catch (JSONException ex) {
        return null;
    }
    return xxx;
}
Sign up to request clarification or add additional context in comments.

4 Comments

though you are right but only when i know string is valid array, what if get that string as method arguement.please see my edit
edited. You just need to handle this exception and make some decision to return null, or throw exception by your method (and handle later).
that solution is provided by @Ankur Lathi. I am looking for solution using this API rather than making decision on basis of exception.Though i think so it is the only approach left.
You don't really need to make special check if this is valid string because you receive this information from exception.
1

You can check it like this:

public boolean isValid(String jsonString)
{
    boolean valid = false;
    try {
        JSONArray.fromObject(jsonString)
        valid = true;
    }
    catch(JSONException ex) { 
        valid = false;
    }
    return valid;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.