Android==>json parsing

Source: Internet
Author: User

public class Jsonutil {


/**
* get "array form" JSON data, data form: [{"id": 1, "name": "nickname"},{"id": 2, "name": "Xiao Li"}]
*
* @param path
* Web Path
* @return Back to List
* @throws Exception
*/
public static string Getjsonarray (string path) throws Exception {
String json = NULL;
URL url = new URL (path);
// LOG.D ("", "*********************url=" +url+ "**************************");
HttpURLConnection conn = (httpurlconnection) url.openconnection ();//using the HttpURLConnection object, we can get the Web page data from the network.
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("User-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setconnecttimeout (20000);
Conn.setreadtimeout (20000);
if (conn.getresponsecode () = = 200) {//Determine if the request code is 200 yards, otherwise failed
InputStream is = Conn.getinputstream (); Get input stream
byte[] data = Readstream (IS); Convert input flow to character array
JSON = new String (data); Converts a character array to a string
}
return JSON;
}


/**
* Convert input flow to character array
*
* @param inputstream
* Input Stream
* @return Character array
* @throws Exception
*/
public static byte[] Readstream (InputStream inputstream) throws Exception {
Bytearrayoutputstream bout = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len = 0;
while (len = inputstream.read (buffer))! =-1) {
Bout.write (buffer, 0, Len);
}
Bout.close ();
Inputstream.close ();


return Bout.tobytearray ();
}

/**
* Check if there is an int for the corresponding field in Jsonobject and return the corresponding value if it exists
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static int Checkandgetint (Jsonobject object, String val)
Throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Object.getint (Val);
} else {
return-1;
}
} catch (Jsonexception e) {
return-1;
}
}


/**
* Check if there is an int for the corresponding field in Jsonobject and return the corresponding value if it exists
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static int Checkandgetintwithdefault (Jsonobject object, String Val,
int result) throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Object.getint (Val);
} else {
return result;
}
} catch (Jsonexception e) {
return result;
}
}


/**
* check if a string of the corresponding field exists in Jsonobject and return the corresponding value if it exists
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static string Checkandgetstring (Jsonobject object, String val)
Throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Object.getstring (Val);
} else {
Return "";
}
} catch (Jsonexception e) {
Return "";
}
}


/**
* check if a string of the corresponding field exists in Jsonobject, return the corresponding value if it exists, otherwise return the default value
*
* @param Object
* @param val
* @param value
* @return
* @throws jsonexception
*/
public static String Checkandgetstringwithdefault (Jsonobject object,
String val, String value) throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Object.getstring (Val);
} else {
return value;
}
} catch (Jsonexception e) {
return value;
}
}


/**
* Check if there is a corresponding field jsonarray in Jsonobject if there is a corresponding value returned
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static Jsonarray Checkandgetarray (Jsonobject object, String val)
Throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Object.getjsonarray (Val);
} else {
return new Jsonarray ();
}
} catch (Jsonexception e) {
return new Jsonarray ();
}
}


/**
* Check if there is a double for the corresponding field in Jsonobject, if there is a return value
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static Double checkandgetdouble (Jsonobject object, String val)
Throws Jsonexception {
try {


if (Object.has (val) && object.get (val) = null) {
Return Object.getdouble (Val);
} else {
return-1.0;
}
} catch (Jsonexception e) {
return-1.0;
}
}


/**
* Check Jsonobject for the existence of the corresponding field of float, if there is a return value
*
* @param Object
* @param val
* @return
* @throws org.json.JSONException
*/
public static Float Checkandgetfloat (Jsonobject object, String val)
Throws Jsonexception {
try {
if (Object.has (val) && object.get (val) = null) {
Return Float.parsefloat (Object.getstring (Val));
} else {
return-1.0f;
}
} catch (Jsonexception e) {
return-1.0f;
}
}
}

Android==>json parsing

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.