Here are some examples of what I do with JSON based on Gson, and now let me say how Gson json is parsed.
Create a new project:
Weatherdemo, import the Gson package. After the Gson library import is complete, it starts
Define an entity class to fit the data you want
Package cn.com.shenzhen.weather.parse;
public class UserInfo {
public String name;
Public String age;
public int height;
}
The following is a test class that parses JSON and parses JSON
Package cn.com.shenzhen.weather.parse;
Import Com.google.gson.Gson;
public class Gsontest {
public static void Main (string[] args) {
Testtojson ();
Testfromjsonstr ();
}
private static void Testfromjsonstr () {
Gson Gson = new Gson ();
UserInfo UserInfo = Gson.fromjson ("{\" name\ ": \" mayubao\ ", \" age\ ": \" 10\ ", \" height\ ":}", Userinfo.class);
System.out.println (Userinfo.name);
System.out.println (Userinfo.age);
System.out.println (Userinfo.height);
}
private static void Testtojson () {
Gson Gson = new Gson ();
UserInfo UserInfo = new UserInfo ();
Userinfo.name = "Mayubao";
Userinfo.age = "10";
Userinfo.height = 170;
String userjsonstr = Gson.tojson (UserInfo);
System.out.println (USERJSONSTR);
}
}
Gson parsing JSON