开发者

How to deserialize an PHP array with GSON

In PHP i serialize an array with json_encode that i try to read on my Android device with GSON. But Android crashes with a fatal exception. I can't get it to work with any of the examples that i've seen. I think it is caused by the JSON string. It starts with the index of the array (19 and 17 in the example below), and i haven't seen any examples of this type of JSON string. All the examples that i have seen start with property names and not with a index / cpunter.

Here's my JSON example:

{"19":{"id":"1262","username":"abc","telnr":"0123456789","created":"Sun, 08 Aug 2010 10:29:06 +0200","email":"e@mail.com","code":null},"17":{"id":"1259","username":"cdef","telnr":"9876543210","created":"Sun, 08 Aug 2010 10:29:06 +0200","email":"mail@domain.com","code":null},

Among many other combinations i tried the following two examples:

Type listType = new TypeToken<List<String>>(){}.getType(); 
List<String> personList = new Gson().fromJson(json, listType);

and

Type playerType = new TypeToken<List<Player>>(){}.getType(); 
List<Player> players = new Gson().fromJson(json.toString(), playerType);

i get the follow error:

threadid=1: thread exiting with uncaught exception
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}:java.lang.NullPointerException

I have a Player class / bean that can hold the id, username, telnr, created,开发者_如何转开发 email and code properties. I hope somebody can provide me a example of the GSON code that will work on this JSON string. Thanks.


The names of properties in a JSON object (the "19" and "17" above) must be valid Javascript identifiers. They can contain only letters, digits, underscores, and dollar signs, but they may not start with a digit. You must assign different keys or remove the keys altogether to form a Javascript array.

$values = array_values($arrayWithKeys);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜