Gson is a kind of object parsing json, which is very easy to use. We introduce a website http://www.php.cn/ that can help us see whether a string is Json
For Json files
{ "id" : "3232", "data" : { "data1" : { "name" : "xiaoming", "age" : "12" } } }
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 <strong>id</strong>; Data <strong>data</strong>; } public class Data { Data1 <strong>data1</strong>; } public class Data1 { String <strong>name</strong>; String <strong>age</strong>; }
Note that the name of the member variable must be The same as the name of the node (bold characters)
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(); } }