开发者

JSON handling Array and object with same Java code

I have a simple question for android/java implementing JSON. Is there any meth开发者_运维技巧od possible to handle an array or object with the same code. In my Json response there are many placed where JSON returns a single argument and some times many arguments in an array. I will have to write a long code to handle whether object contains an array or an object otherwise. Also if somebody help me, how to check whether a element contains JSONArray or JSONObject?


Simply test (instanceof) the type of the object you get back from the JSON parser to see whether it's an array, dictionary, or single object.

If you want to avoid duplication where the array is an array of the instances you'd get back in the simpler case, first test for non-array, and if that's what you have, create an array and stick the single object into it, before passing it to your generalized array-handling logic.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜