Android uses gson to parse nested multi-layer JSON

First of all, let's talk about a relatively simple example (I won't talk about the simplest, there are many on the Internet) to help novices understand the usage of gson:

For example, we want to parse a JSON like this:

First, we need to define a serialized bean in the form of an internal class, which looks clearer:

Many times, we don't know how to define this bean. Here are some points to pay attention to:

1. Internally nested classes must be static, or there will be errors in parsing;

2, as like as two peas in the Json field, the attribute names in the class must be exactly the same as those in the Key field.

3. The internal nested part enclosed by [] is a list, so it is defined as public list < b > b, while the part nested only by {} is defined as public C,

Specifically, you can see by comparing the JSON string. If you don't understand, we can communicate with each other. I'm also a novice in development!

Then it's easy to get the data. Just get it directly in jsonbean!

If the JSON to be resolved is nested in many layers, you can also define a bean nested in many layers of internal classes. You need to carefully define it by referring to the JSON field.

The above simple way for Android to use gson to parse nested multi-layer JSON is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>