Android parses json and jsonreaderjson using jsonReader
For this json:
{ "id" : "3232", "data" : [{ "data1" : "555", "data2" : "3243" }, { "data1" : "888", "data2" : "777" }] }
We can resolve it as follows:
import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.io.StringReader;import com.google.gson.stream.JsonReader;import android.app.Activity;import android.content.Context;import android.os.Bundle;public class TestActivity1 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);parseAssertData();}public void parseAssertData() {InputStream is = null;try {is = this.getAssets().open("ss.json", Context.MODE_PRIVATE);int length = is.available();byte[] buffer = new byte[length];is.read(buffer);String temp = new String(buffer);Reader response = new StringReader(temp.toString());parseResponse(response);} catch (IOException ex) {ex.printStackTrace();}}private void parseResponse(Reader response) throws IOException {JsonReader reader = new JsonReader(response);reader.beginObject();while (reader.hasNext()) {String name = reader.nextName();if ("id".equals(name)) {String id = reader.nextString();System.out.println("===id="+id);} else if (name.equals("data")) {reader.beginArray();while (reader.hasNext()) { reader.beginObject(); String name1; while (reader.hasNext()) { name1 = reader.nextName(); if (name1.equals("data1")) { String s1 = reader.nextString(); System.out.println("===s1="+s1); } else if (name1.equals("data2")) { String s2 = reader.nextString(); System.out.println("===s2="+s2); } else { reader.skipValue(); } } reader.endObject();}reader.endArray();}else {reader.skipValue();}}reader.endObject();reader.close();}}
How does Android parse json data?
Let's take a look at it for you. The approximate resolution method is as follows:
As follows:
Privatevoid testJson (){
String JsonData = "[{\" id \ ": [\" 386 \ ", \" 381 \ ", \" 379 \ ", \" 377 \ "], \ "num \": [\ "386 \", \ "381 \", \ "379 \", \ "377 \"] },{ \ "id \": [\ "3860 \", \ "3810 \", \ "3790 \", \ "3770 \"], \ "num \": [\ "3860 \", \ "3810 \", \ "3790 \", \ "3770 \"]}] ";
JSONObject obj = null;
JSONArray jsonArary;
Try {
JsonArary = new JSONArray (JsonData );
For (int I = 0; I <jsonArary. length (); I ++ ){
Obj = jsonArary. getJSONObject (I );
// Obtain the ID
JSONArray strID = obj. getJSONArray ("id ");
For (int j = 0; j <strID. length (); j ++ ){
Log. v ("ID:", strID. get (j). toString ());
}
// Obtain num
JSONArray strNum = obj. getJSONArray ("num ");
For (int k = 0; k <strNum. length (); k ++ ){
Log. v ("NUM:", strNum. get (k). toString ());
}
}
} Catch (JSONException e ){
E. printStackTrace ();
}
}
How to parse JSON data like this in android,
// Json: I understand a series of KEY-value pairs. If the KEY value is correct and the corresponding data format is correct, you can !~ JsonObject j =... // here is the method for obtaining json, custom boolean B = j. getBoolean ("result"); // retrieve the value corresponding to "result". The result here should be // "true"; JsonArray ja = j. getJsonArray ("asks"); // retrieve the value corresponding to "asks". Because the value of asks contains square brackets, this is JsonArray data and needs to be parsed further; jsonArray can be compared with arrays //. I personally use this for reference (int I = 0; I <ja. length (); I ++) {JsonArray ja1 = ja. getJsonArray (I); // obtain the first element in ja. Because this element // is also a jsonArray, You can further parse double d1 = ja1.getDouble (0 ); // The getFloat () method is not available in jsonArray. // you can perform the transformation on your own. double d2 = ja1.getDouble (1); //} and above. I hope it will help you.