开发者

JSONObject exceptions

I am using a JSON array to process data back n forth between layers. I have been getting an exception on JSONObject. Could you please guide me towards the correct debug path for this issue?

First approach:

for( Object obj : salesDataArray )
{
 JSONObject salesData = (JSONObject) obj;

Exception I am getting:

java.lang.ClassCastException: java.lang.String incompatible with net.sf.json.JSONObject

Second approach:

for( Object obj : salesDataArray )
{
JSONObject salesData = JSONObject.fromObject(obj);

Exception I am getting:

net.sf.json.JSONException: 开发者_开发知识库Unquotted string 'undefined'


Your salesDataArray is an array of String objects, not JSONObject objects. Where is this being populated? Best best is to print out the values of the salesDataArray and find out what the strings are, and look upstream for the reason that they are set that way.

Also, consider using generics to type your salesDataArray, like

List<JSONObject> salesDataArray = ....  // get the sales data from somewhere


System.out.println((String) obj);

Sounds like you don't have valid JSON data in there in the first place if you have 'undefined' in it. Javascript undefined is like 'null' in Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜