开发者

JSON to multidimentional array in android/java

I'm looking into creating a library for myself that handles common internet-related functions. So far I have http GET and POST functions, and now I'm looking into developing a one-size-fits-all JSON parsing function that will convert JSON it receives as a string into a multi-dimentional associative array that can be searched through.

I looked through stackoverflow and found this link which looks very helpful, other than that the code is designed to be very specific. Is there a nice handy function in org.json.* that can let you turn your JSON string into a multi-dimentional associative array that I can return and manipulate outside the class?

Thanks, John.

Ps. I should mention that I have very little prior experie开发者_StackOverflow社区nce with java - I'm a PHP developer at heart.


I don't think there's a need to write your own JSON library. Android comes with a lot you can use, or you can use GSON which I've had great success with: http://code.google.com/p/google-gson/

See how easy it is:

// Serialise
String[] array = {"One","Two","Three"};
String json = new Gson().toJson(array);

// De-serialise
String[] array = new Gson().fromJson(json,String[].class);

GSON works just fine with a whole range of classes including your own class objects, arrays, and some collections.

One caveat is that the GSON library does not support the date format used by APIs on Microsoft web servers, but in those scenarios I've just pulled the data down into a String var and then process is inside the class to a date using my own parsing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜