android uses Gson to parse json

黄舟
Release: 2017-02-20 14:49:56
Original
1345 people have browsed it

Gson is a kind of object parsing json, which is very easy to use. We introduce a website //m.sbmmt.com/ that can help us see whether a string is Json

For Json files


{ "id" : "3232", "data" : { "data1" : { "name" : "xiaoming", "age" : "12" } } }
Copy after login


If you use Gson to parse, you must define the class corresponding to this json node. We use MyData to represent the parsed json object, and Data to represent the parsing. After completing the object of the data node, the Data1 class represents the object of the data1 node



##

public class MyData { int id; Data data; } public class Data { Data1 data1; } public class Data1 { String name; String age; }
Copy after login

Note that the name of the member variable must be The same as the name of the node (bold characters)


We put the json file under assets and write it like this when parsing:


public void parseAssertData() { InputStream is = null; try { is = this.getAssets().open("ss.json", Context.MODE_PRIVATE); int length = is.available(); byte[] buffer = new byte[length]; is.read(buffer); String temp = new String(buffer); Reader response = new StringReader(temp.toString()); Gson gson = new Gson(); MyData mydata = gson.fromJson(response,MyData.class); System.out.println("===age="+mydata.data.data1.age); } catch (IOException ex) { ex.printStackTrace(); } }
Copy after login
The above is the content of android using Gson to parse json. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!