Gson parsing of Android JSON

Source: Internet
Author: User

First, Google Gson this Java class library can convert Java objects to JSON, or you can convert a JSON string into an equal Java object. Gson supports any complex Java object, including objects without source code.


second, Gson parsing json step
A, server-side convert data to JSON string
First, the server-side project will import the Gson jar package into Builtpath. (
Gson jar:http://code.google.com/p/google-gson/ We can also download the Gson help document )






The data is then converted to a JSON string, with the core function:
public static String createjsonstring (Object value)
{
Gson Gson = new Gson ();
String str = Gson.tojson (value);
return str;
}
B. The client converts the JSON string to the corresponding JavaBean
first, the client also imports the Gson two jar package , the JSON jar does not need to be imported (because the JSON jar package is already integrated in the Android project, there is no need to import)
1. The client gets the JSON string
public class Httputil
{

public static string Getjsoncontent (String urlstr)
{
Try
{//Get HttpURLConnection Connection object
URL url = new URL (urlstr);
HttpURLConnection httpconn = (httpurlconnection) URL
. OpenConnection ();
Setting connection Properties
Httpconn.setconnecttimeout (3000);
Httpconn.setdoinput (TRUE);
Httpconn.setrequestmethod ("GET");
Get the corresponding code
int respcode = Httpconn.getresponsecode ();
if (Respcode = = 200)
{
Return Convertstream2json (Httpconn.getinputstream ());
}
}
catch (Malformedurlexception e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
catch (IOException E)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
Return "";
}


private static String Convertstream2json (InputStream inputstream)
{
String jsonstr = "";
Bytearrayoutputstream equivalent to memory output stream
Bytearrayoutputstream out = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len = 0;
Transfer the input stream to the memory output stream
Try
{
while (len = inputstream.read (buffer, 0, buffer.length))! =-1)
{
Out.write (buffer, 0, Len);
}
Converting a memory stream to a string
Jsonstr = new String (Out.tobytearray ());
}
catch (IOException E)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
return jsonstr;
}
}
2. Using generics to get JavaBean(Core function)
public static <T> T Getperson (String jsonstring, class<t> CLS) {
T t = null;
try {
Gson Gson = new Gson ();
t = Gson.fromjson (jsonstring , CLS);
} catch (Exception e) {
Todo:handle exception
}
return t;
}

public static <T> list<t> getpersons (String jsonstring, class<t> CLS) {
list<t> list = new arraylist<t> ();
try {
Gson Gson = new Gson ();
list = Gson.fromjson (Jsonstrin G, new typetoken<list<cls>> () {
}.gettype ()) ;
} catch (Exception e) {
}
return list;
}
public static list<map<string, object>> listkeymaps (String jsonstring) {
list<map<string, object>> list = new arraylist<map<string, object>> ();
try {
Gson Gson = new Gson ();
list = Gson.fromjson (jsonstr Ing
             new typetoken<list<map<string , object>> > () {
             }.gettype ( ) );
} catch (Exception e) {
Todo:handle exception
}
return list;
}

Gson parsing of Android 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.