Gson parsing JSON

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.