JSON parsing for Android

Source: Internet
Author: User
Tags tojson

Turn http://blog.csdn.net/qxs965266509/article/details/42774691How to use Google Gson to convert JSON structures to each otherCategory: Android Json2015-01-16 12:03 884 people read reviews (0) favorite reports

In Java development, it is sometimes necessary to save a data structure into a string, you might consider using JSON, but when the JSON string is converted to a Java object, it is converted to Jsonobject, not the object of the class type you want, and it is not pleasant to operate. This problem can be solved by the following words.

First, you need to import Google's Gson jar package into the project, the simple steps of this import package will not show, Gson download link: http://download.csdn.net/detail/qxs965266509/8367275

Now, I'll start by customizing a class

[Java]View Plaincopy
    1. Public class Student {
    2. public int id;
    3. Public String nickname;
    4. public int age;
    5. Public arraylist<string> Books;
    6. Public hashmap<string, string> booksmap;
    7. }

Case one, case two, Case San du is a string that converts a Java class object using Gson to JSON

Case one:

Data structures that contain only basic data types

[Java]View Plaincopy
    1. Gson Gson = new Gson ();
    2. Student Student = new Student ();
    3. Student.id = 1;
    4. Student.nickname = "Choshaosong";
    5. Student.age = 22;
    6. Student.email = "[email protected]";
    7. LOG.E ("mainactivity", Gson.tojson (student));

The output is:

[Java]View Plaincopy
    1. {"email": "[email protected]", "nickname":"Choshaosong","id":1,"age":

Case TWO:

The list collection is included in addition to the basic data type

[Java]View Plaincopy
  1. Gson Gson = new Gson ();
  2. Student Student = new Student ();
  3. Student.id = 1;
  4. Student.nickname = "Choshaosong";
  5. Student.age = 22;
  6. Student.email = "[email protected]";
  7. arraylist<string> books = new arraylist<string> ();
  8. Books.add ("mathematics");
  9. Books.add ("language");
  10. Books.add ("English");
  11. Books.add ("physical");
  12. Books.add ("chemistry");
  13. Books.add ("Biology");
  14. Student.books = books;
  15. LOG.E ("mainactivity", Gson.tojson (student));
The output is: [HTML]View Plaincopy
    1. {"Books": ["Mathematics", "language", "English", "Physics", "Chemistry", "biology"], "email": "[email protected]", "nickname": "Choshaosong", "id": 1, "Age": 22}

Case THREE:

The list and map collections are included in addition to the basic data types

[Java]View Plaincopy
  1. Gson Gson = new Gson ();
  2. Student Student = new Student ();
  3. Student.id = 1;
  4. Student.nickname = "Choshaosong";
  5. Student.age = 22;
  6. Student.email = "[email protected]";
  7. arraylist<string> books = new arraylist<string> ();
  8. Books.add ("mathematics");
  9. Books.add ("language");
  10. Books.add ("English");
  11. Books.add ("physical");
  12. Books.add ("chemistry");
  13. Books.add ("Biology");
  14. Student.books = books;
  15. hashmap<string, string> booksmap = new hashmap<string, string> ();
  16. Booksmap.put ("1", "mathematics");
  17. Booksmap.put ("2", "language");
  18. Booksmap.put ("3", "English");
  19. Booksmap.put ("4", "physical");
  20. Booksmap.put ("5", "chemistry");
  21. Booksmap.put ("6", "creature");
  22. Student.booksmap = Booksmap;
  23. LOG.E ("mainactivity", Gson.tojson (student));

The output is:

[Java]View Plaincopy
  1. { "books": [ "language", "Booksmap": { "2": "math", "physical"}, "email": "[email protected]", "nickname": "Choshaosong", 1,< Span class= "string" "Age": 22}&NBSP;&NBSP;

Case FOUR:
Convert a case three-output string to a student object using Gson

[Java]View Plaincopy
  1. Gson Gson = new Gson ();
  2. Student Student = new Student ();
  3. Student.id = 1;
  4. Student.nickname = "Choshaosong";
  5. Student.age = 22;
  6. Student.email = "[email protected]";
  7. arraylist<string> books = new arraylist<string> ();
  8. Books.add ("mathematics");
  9. Books.add ("language");
  10. Books.add ("English");
  11. Books.add ("physical");
  12. Books.add ("chemistry");
  13. Books.add ("Biology");
  14. Student.books = books;
  15. hashmap<string, string> booksmap = new hashmap<string, string> ();
  16. Booksmap.put ("1", "mathematics");
  17. Booksmap.put ("2", "language");
  18. Booksmap.put ("3", "English");
  19. Booksmap.put ("4", "physical");
  20. Booksmap.put ("5", "chemistry");
  21. Booksmap.put ("6", "creature");
  22. Student.booksmap = Booksmap;
  23. String result = Gson.tojson (student);
  24. Student STUDENTG = Gson.fromjson (result, Student.   Class);
  25. LOG.E ("mainactivity", "ID:" + studentg.id);
  26. LOG.E ("mainactivity", "nickname:" + Studentg.nickname);
  27. LOG.E ("mainactivity", "Age:" + studentg.age);
  28. LOG.E ("mainactivity", "email:" + studentg.email);
  29. LOG.E ("mainactivity", "books size:" + studentG.books.size ());
  30. LOG.E ("mainactivity", "Booksmap size:" + studentG.booksMap.size ());

It is important to note that:

1, because the JSON to the class when some data have or not, it is best to JavaBean class, the subclass of the class can be set to common.

2, when the JSON data from the server is very complex, you can use the Hijson tool to parse out the class schema, and then to build the class.

JSON parsing for Android

Related Article

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.