Data structure of JSON
For example:
[
???? {????
???????? "AuthType": "OAuth2",
???????? "CAS": "https://auth.bistu.edu.cn",
???????? "OAuth2": "https://222.249.250.89:8443",
???????? "Androidupgrade": "http://m.bistu.edu.cn/upgrade/Android.php",
???????? "Jwapi": "Http://m.bistu.edu.cn/jiaowu",
???????? "Icampusapi": "Http://m.bistu.edu.cn/api",
???????? "Newsapi": "Http://m.bistu.edu.cn/newsapi"
????}
]
In this set of data, the content in [] is a JSON array, if there are multiple [], then there are multiple JSON arrays; {} The data is a JSON object, if there is more than one {}, then it indicates that there are multiple JSON objects in the JSON array, one object has multiple key-value pairs, The key before the colon, the value after the colon, and the different key-value pairs, separating the
?
Parsing of JSON
Secondactivity.java
PackageCom.ahtcfg24;
ImportAndroid.app.Activity;
ImportAndroid.os.Bundle;
ImportAndroid.widget.TextView;
ImportCom.loopj.android.http.AsyncHttpClient;
ImportCom.loopj.android.http.AsyncHttpResponseHandler;
ImportOrg.apache.http.Header;
ImportOrg.json.JSONArray;
ImportOrg.json.JSONException;
ImportOrg.json.JSONObject;
/**
* Created by Ahtcfg24 on 2015/4/12.
*/
Public classSecondactivityextendsActivity
{
PrivateTextView;
@Override
protected voidOnCreate(BundleSavedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setcontentview (R.layout.Layout);
TextView=(TextView) Findviewbyid (R.id.TextView2);
AsynchttpclientClient=NewAsynchttpclient ();
Client. Get ("Http://m.bistu.edu.cn/icampus_config.json",NULL,NewAsynchttpresponsehandler () {
@Override
Public voidOnFailure(intarg0header[] Arg1 byte [] arg2< Span style= "COLOR: #cc7832",
throwable arg3) {
}
@Override
public void onsuccess ( int Span style= "COLOR: #a9b7c6" >statuscodeheader[] headers< Span style= "COLOR: #cc7832", byte [] Responsebody) {
/* get the URL from the JSON Number data converted to string format by byte array*/
String Data = new string (responsebody);
System. out . println (" This is the string+data); /*Debug*/
Try {
/*jsonarray is constructed by taking the array out of the string and putting it in the Jsonarray .*/
Jsonarray= new jsonarray (data);
System. out . println (" this isJSONthe first element of an array"+Jsonarray. Getjsonobject (0));
/*The first element in the array (the firstThe JSON object is extracted and assigned to a jsonobject, so this jsonobject is stored in a JSON data) */
Jsonobject= jsonarray. Getjsonobject (0);
/* The corresponding value is found by the key name by the getString method*/
System. out. println (jsonobjectgetString ("Newsapi"));
TextView. SetText (jsonobjectgetString ("Newsapi"));
} catch (jsonexception e) {
E.printstacktrace ();
}
}
});
}
}
?
Layout.xml
<?XML version="1.0"encoding="Utf-8"?>
<LinearLayoutxmlnsAndroid="Http://schemas.android.com/apk/res/android"
Android: layout_width= "match_parent"
android: Layout_height= "match_parent"
android:p adding=" 10DP "
android:orientation=" vertical ">
< TextView
android :layout_width= "wrap_content"
android:layout_height= " Wrap_content "
Android:text= NULL"
Android: textcolor="#000000"
Android: textsize="50SP"
Android: layout_gravity="center"
Android: id="@+id/textview2" />
</linearlayout>
?
Android parsing JSON