Json data analysis Gson

报错: Caused by: java.lang.IllegalStateException: Expected a double but was BEGIN_OBJECT at line 1 column 101 path $.data.datastreams[0].datapoints[0].value

Reason: JSON data parsing error.

Solution: JSON data is divided into two types - objects and arrays. Objects are enclosed in {}, and arrays are enclosed in []. I performed data analysis through Gson. According to the reported value, I learned that my value is an object. I need to add a value class based on the original analysis class. After clarifying the relationship, I re-wrote the gson data analysis. Classes and logical relationships are easily confused when sorted out. You can also directly generate the required classes based on the reported data and then use the json conversion tool (on the basis of understanding, this method is more recommended to save time.), but also pay attention to Change the corresponding data acquisition method in the main class (the class that obtains json parsed data). Changes I made in the main class after the changes: watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Yqq5Yqb5a2m5Lmg55qE5b-D5a2Q,size_15,color_FFFFFF,t_70,g_se,x_16watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Yqq5Yqb5a2m5Lmg55qE5b-D5a2Q,size_8,color_FFFFFF,t_70,g_se,x_16

 Extension: One of the ways to parse Json is through Gson. Gson is a Java class library provided by Google for mapping between Java objects and Json data. It can convert Java objects and Json data to each other. You need to import gson when using it. .jar package, placed in the lib folder, remember to "Add as library" after importing.

Json data is divided into two types - objects and arrays. Objects are enclosed in {}, and arrays are enclosed in []. Json arrays should be saved using List.

Parse Json object: Gson gson = new Gson(); User user=gson.fromJson(jsonData,User.class);

解析Json数组:Gson gson=new Gson();List<User> userList=gson.fromJson(jsonData, new TypeToken<User>(){}.getType());

 

おすすめ

転載: blog.csdn.net/weixin_58222015/article/details/124487554