SoFunction
Updated on 2025-04-08

Detailed summary of JSON in Android


import ;
import ;
import .*;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class JSON {

/**
* Get JSON data in "array form",
* Data form: [{"id":1,"name":"pig"},{"id":2,"name":"kitten"}]
* @param path webpage path
* @return Return List
* @throws Exception
*/
public static List<Map<String, String>> getJSONArray(String path) throws Exception {
String json = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) ();// Using the HttpURLConnection object, we can obtain web page data from the network.
(5 * 1000); // The unit is milliseconds, the timeout is set to 5 seconds
("GET"); // HttpURLConnection requests the path path through the HTTP protocol, so you need to set the request method, you can not set it because the default is GET
if (() == 200) {// Determine whether the request code is 200 code, otherwise it will fail
InputStream is = (); // Get input stream
byte[] data = readStream(is); // Convert input stream into a character array
json = new String(data); // Convert character array into string

//Data form: [{"id":1,"name":"pig","age":22},{"id":2,"name":"kitten","age":23}]
JSONArray jsonArray = new JSONArray(json); //The data is directly in an array form, so you can directly use the framework JSONArray provided by Android to read JSON data and convert it into Array

for (int i = 0; i < (); i++) {
JSONObject item = (i); //Each record is composed of several Object objects
int id = ("id"); // Get the corresponding value of the object
String name = ("name");

map = new HashMap<String, String>(); // Save it in MAP
("id", id + "");
("name", name);
(map);
}
}

// ************ Test data*********************
for (Map<String, String> list2 : list) {
String id = ("id");
String name = ("name");
("abc", "id:" + id + " | name:" + name);
}
return list;
}

/**
* Get JSON data in "object form",
* Data form: {"total":2,"success":true,"arrayData":[{"id":1,"name":"pig"},{"id":2,"name":"kitten"}]}
* @param path webpage path
* @return Return List
* @throws Exception
*/
public static List<Map<String, String>> getJSONObject(String path) throws Exception {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) ();// Using the HttpURLConnection object, we can obtain web page data from the network.
(5 * 1000); // The unit is milliseconds, the timeout is set to 5 seconds
("GET"); // HttpURLConnection requests the path path through the HTTP protocol, so you need to set the request method, you can not set it because the default is GET
if (() == 200) {// Determine whether the request code is 200 code, otherwise it will fail
InputStream is = (); // Get input stream
byte[] data = readStream(is); // Convert input stream into a character array
String json = new String(data); // Convert character array into string


//Data form: {"total":2,"success":true,"arrayData":[{"id":1,"name":"pig"},{"id":2,"name":"kitten"}]}
JSONObject jsonObject=new JSONObject(json); //The returned data form is an Object type, so it can be directly converted into an Object
int total=("total");
Boolean success=("success");
("abc", "total:" + total + " | success:" + success); //Test data

JSONArray jsonArray = ("arrayData");//There is an array of data inside, you can use getJSONArray to get the array
for (int i = 0; i < (); i++) {
JSONObject item = (i); // Get each object
int id = ("id"); // Get the corresponding value of the object
String name = ("name");

map = new HashMap<String, String>(); // Save it in MAP
("id", id + "");
("name", name);
(map);
}
}

// ************ Test data*********************

for (Map<String, String> list2 : list) {
String id = ("id");
String name = ("name");
("abc", "id:" + id + " | name:" + name);
}
return list;
}


/**
* Get JSON data of complex types
*Data form:
{"name":"Piggy",
"age":23,
"content":{"questionsTotal":2,
"questions": [ { "question": "what's your name?", "answer": "piglet"},{"question": "what's your age", "answer": "23"}]
}
}
* @param path webpage path
* @return Return List
* @throws Exception
*/
public static List<Map<String, String>> getJSON(String path) throws Exception {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) ();// Using the HttpURLConnection object, we can obtain web page data from the network.
(5 * 1000); // The unit is milliseconds, the timeout is set to 5 seconds
("GET"); // HttpURLConnection requests the path path through the HTTP protocol, so you need to set the request method, you can not set it because the default is GET
if (() == 200) {// Determine whether the request code is 200 code, otherwise it will fail
InputStream is = (); // Get input stream
byte[] data = readStream(is); // Convert input stream into a character array
String json = new String(data); // Convert character array into string


/*Data form:
{"name":"Piggy",
"age":23,
"content":{"questionsTotal":2,
"questions": [ { "question": "what's your name?", "answer": "piglet"},{"question": "what's your age", "answer": "23"}]
}
}
*/
JSONObject jsonObject=new JSONObject(json); //The returned data form is an Object type, so it can be directly converted into an Object
String name=("name");
int age=("age");
("abc", "name:" + name + " | age:" + age); //Test data

JSONObject contentObject=("content"); //Get the object in the object
String questionsTotal=("questionsTotal"); //Get a value in the object
("abc", "questionsTotal:" + questionsTotal); //Test data

JSONArray contentArray=("questions"); //Get the array in the object
for (int i = 0; i < (); i++) {
JSONObject item = (i); // Get each object
String question = ("question"); // Get the corresponding value of the object
String answer = ("answer");

map = new HashMap<String, String>(); // Save it in MAP
("question", question);
("answer", answer);
(map);
}
}

// ************ Test data*********************

for (Map<String, String> list2 : list) {
String question = ("question");
String answer = ("answer");
("abc", "question:" + question + " | answer:" + answer);
}
return list;
}




/**
* Convert input stream into 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 = (buffer)) != -1) {
(buffer, 0, len);
}
();
();

return ();
}
}