1

Just starting so any facilitation appreciated. Have an object serialized it to this starting segment:

[{"boo00":true,"lineItem":[{"bool00":true,"display":false,"extra00":"objectType","status":"","value":"recordType:A"}, {"bool00":true,"display":false,"extra00":"0","status":"","value":"UPC:1"},{...},{...}],"rowId":0,"str00":"hidden"},

The object has four elements: boo00 (boolean), lineIem (ArrayList), rowID (int), str00 (String)

I have this segment where I am trying to understand how to build back my object

String flatten = lines.toString();
JSONObject outer = JSONObject.fromObject(flatten);
Iterator itr04 = null;
for(itr04 = outer.entrySet().iterator(); itr04.hasNext();){
 Map.Entry entry = (Map.Entry) itr04.next();
 String key = (String)entry.getKey();
 System.out.println("key= "+key);
}

I am getting the following exception:

"net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of []"

which at this point makes little sense to me because that is exactly how it starts.

Mac
14.8k11 gold badges65 silver badges83 bronze badges
asked Sep 15, 2012 at 2:07

2 Answers 2

3

Your JSON text begins with a square bracket: [, not a curly brace: {. The JSON string represents an array of objects.

Your outer should be a JSONArray.

answered Sep 15, 2012 at 2:14
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.If I can follow up here goes. How to I get the next "chunk"? JSONArray.size() shows three elements of my object type. Now since I know the structure how to I parse recursively into each? In this case where I have one object that contains some primitives and an ArrayList, how do get that as a separated chunk and parse that and iterate the list within?
@cp - If you know you have a JSONArray, you can iterate through it and extract each element. If you know that every element will be a JSONObject then you can just cast to that. If you're not sure, use instanceof to test the type of the array element before casting.
0

Since the Java JSON stuff is sort of brain dead, one thing you can do is concatenate "[" and "]" to the front and end of the string. Then you can always use JSONArray to read the string, and then do instanceof on each array element to find out what you have.

answered Sep 15, 2012 at 2:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.