SoFunction
Updated on 2025-03-02

Android programming method to parse Json format data

This article describes the Android programming method of parsing Json format data. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class JsonDemo extends Activity {
 /*
   * Example of parsing JSON, str saves JSON code, and the parsed data is output in LogCat
  */
 String TAG = "Json message";
 @Override
 public void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView();
  detectJSON();
 }
 private void detectJSON() {
  String str = "{"+
   "\"date\" : \"2011-06-06\","+
   //Like is JSONObject   "\"Like\" : {"+
   "\"Name\" : \"Garnett\","+
   "\"Height\" : \"2.11cm\","+ 
   "\"Age\" : 35"+
   "},"+
   //LikeList is a JSONObject   "\"LikeList\":" +
    "{\"List\": " +
    "["+
     //This is also JSONObject     "{"+
     "\"Name\" : \"Rose\","+
     "\"Height\" : \"190cm\","+ 
     "\"Age\" : 23"+
     "},"+
     //This is also JSONObject     "{"+
     "\"Name\" : \"Kobe","+
     "\"Height\" : \"198cm\","+ 
     "\"Age\" : 33"+
     "}"+
    "]"+
    "}"+
   "}";
  try {
   JSONObject dataJson = new JSONObject(str);
   (TAG, ("date"));
   JSONObject nbaJson = ("Like");
   (TAG, ("Name"));
   (TAG, ("Height"));
   (TAG, ("Age").toString());
   JSONObject listJson = ("LikeList");
   JSONArray arrayJson = ("List");
   for(int i=0;i<();i++) {
    JSONObject tempJson = (i);
    (TAG, ("Name"));
    (TAG, ("Height"));
    (TAG, ("Age").toString()); 
   }
  } catch (JSONException e) {
   ("Something wrong...");
   ();
  }
 }
}

I hope this article will be helpful to everyone's Android programming design.