Gson parses JsonObject, JsonArray, and gsonjsonobject

Source: Internet
Author: User

Gson parses JsonObject, JsonArray, and gsonjsonobject

Important core classes in Gson:

Gson, JsonParser, JsonObject, and JsonArray.

The resolution steps are as follows:

Public void parserJsonArray (String strJson ){
// Create a Gson object
Gson gson = new Gson ();
// Create a JsonParser
JsonParser parser = new JsonParser ();
// Json strings can be parsed into a JsonElement object through the JsonParser object
JsonElement el = parser. parse (strJson );

// Convert a JsonElement object to a JsonObject
JsonObject jsonObj = null;
If (el. isJsonObject ()){
JsonObj = el. getAsJsonObject ();
}


// Convert the JsonElement object to JsonArray
JsonArray jsonArray = null;
If (el. isJsonArray ()){
JsonArray = el. getAsJsonArray ();
}

// Traverse the JsonArray object
Iterator it = jsonArray. iterator ();
While (it. hasNext ()){
JsonElement e = (JsonElement) it. next ();
// Convert JsonElement to a JavaBean object
JbDemo = gson. fromJson (e, JavaBeanDemo. class );
}

}

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.