0

I have a JSON Object which converted into String and saved into database .But when i am trying to get it back it is throwing exception.My object is something like that...

{"COLUMN":["Type","Sub Type","F.P.","P.P.","Process","Due To Start"]}

How can we get the data back in Normal form?

My Java Code is.....

JSONObject obj = new JSONObject();
        JSONArray the_json_array = obj.getJSONArray(userReorderOption);
        int size = the_json_array.size();
        ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
        for (int i = 0; i < size; i++) {
            JSONObject another_json_object = the_json_array.getJSONObject(i);
            arrays.add(another_json_object);
        }

And Exception i am getting....

net.sf.json.JSONException: JSONObject["{\"TASKLIST_COLUMN_REORDER\":[\"Type\",\"Sub Type\",\"F.P.\",\"P.P.\",\"Process\",\"Due To Start\"]}"] is not a JSONArray.

And this is java Code how i am creating JSON Object and saving into database...

String userReorderSelection;  
  Set set = new LinkedHashSet(userReorderSelection);


        JSONObject json = new JSONObject();
        json.accumulate("COLUMN", set);
        saveJSONObj("PrimaryKeyColumn", json.toString());
4
  • 1
    what is the exception? Post it. Commented Oct 9, 2012 at 6:20
  • have you looked at the format in the database? is it the same as the string you tried to write? (Or do you need to escape it) Commented Oct 9, 2012 at 6:25
  • This is what what i got back from database.... {"COLUMN":["Type","Sub Type","F.P.","P.P.","Process","Due To Start"]} Commented Oct 9, 2012 at 7:26
  • I am getting this JSON Object {"COLUMN":"[Sub Type, F.P., P.P., Process, Due To Start, Type]"} with New jar. Looking some issue,now when i am trying to get object back as a String i am getting Exception again....java.lang.ClassCastException: java.lang.String Commented Oct 9, 2012 at 9:15

2 Answers 2

2

Thanks Tichodroma, But as i told i am using net.sf.json.JSONObject class and above things we can achieve from this class too..What i did to solve the above issue?...Please have a look on the Java code...

JSONObject jsonObj = new JSONObject();
        JSONObject obj  = jsonObj.fromObject(userReorderOption);
        JSONArray columnName = (JSONArray) obj.get("COLUMN");

        for (int i = 0; i < columnName.size(); i++) {
            System.out.println(columnName.getString(i));

        }

This code work fine for me with my Json Jar**(net.sf.json)**

Sign up to request clarification or add additional context in comments.

Comments

1

Your JSON is not a JSONArray.

A JSONArray is an ordered sequence of values.

You have a JSONObject.

A JSONObject is an unordered collection of name/value pairs.

Edit:

Using the JSON implementation from org.codehaus.jettison.json, you can do this:

String json = "{\"COLUMN\":[\"Type\",\"Sub Type\",\"F.P.\",\"P.P.\",\"Process\",\"Due To Start\"]}";

JSONObject obj = new JSONObject(json);
JSONArray column = (JSONArray) obj.get("COLUMN");

for (int i = 0; i < column.length(); i++) {
    final String field = column.getString(i);
    System.out.println(field);
}

Result:

Type
Sub Type
F.P.
P.P.
Process
Due To Start

6 Comments

Please let me know how can get value from my JSONObject to a list or StringBuffer
I am using net.sf.json.JSONObject class and when i m trying your code with Constructor like you defined. I am getting ... The constructor JSONObject(String) is undefined.
Thanks i have to add org.codehaus.jettison.json Jar and it worked for me
I am getting another issue when i am trying to create JSON Object with new jar it is creating JSOn Object like this ...{"TASKLIST_COLUMN_REORDER":"[Sub Type, F.P., P.P., Process, Due To Start, Type]"} and when i am trying your code it is throwing case exception.
If you have a new question, please ask a new one :) If you have more details for this question, please add them.
|

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.