Parsing Json Data Format

Source: Internet
Author: User

The text will not be transferred here. Click the link to see it later...
But there is a problem. The several formats he introduced are not what I want. The json format obtained from the server is as follows:
[{"Description": "\ u5e7f \ u544a3", "img": "http: \/clostyle2012.eicp.net \/images \/appimg \/ad \/3.jpg ", "link": "34" },{ "description": "\ u5e7f \ u544a2", "img": "http: \// clostyle2012.eicp.net \/images \/appimg \/ad \/2.jpg", "link": "33" },{ "description": "\ u5e7f \ u544a1 ", "img": "http: \/clostyle2012.eicp.net \/images \/appimg \/ad \/1.jpg"," link ":" 32 "}]
At first glance, we can see that the outside is a JSONArray, And the element is three JSONObject objects. However, if the array does not have a key, you cannot press the one mentioned in the previous article. I have been searching for it for a long time. I have tried it many times and finally got it done. But I don't know if it is the best.
The example format in his article is the same as what I want. I used a part of its code...

[Java]
Package com. Bill_Ming.csdn;
 
Import java. io. ByteArrayOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java.net. HttpURLConnection;
Import java.net. MalformedURLException;
Import java.net. URL;
 
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
 
// Garbled characters appear because index3.jsp saves UTF-8 format from time to time and will be edited using editplus later
Public class JSONDemoActivity extends Activity {

/**
* The backend address of the access, which cannot be accessed locally. 127.0.0.1 should be used 10.0.2.2.
*/
Private static final String BASE_URL = "http: // 10.0.2.2: 8080/index2.jsp ";
// Private static final String BASE_URL = "http://clostyle2012.eicp.net/app/getAd ";
Private TextView mStudentTextView;

Private TextView mClassTextView;


@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Try {
SetupViews ();
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

/**
* Initialization
* @ Throws JSONException
* @ Throws IOException
*/
Private void setupViews () throws JSONException, IOException {
MStudentTextView = (TextView) findViewById (R. id. student );
MClassTextView = (TextView) findViewById (R. id. classes );

Byte [] data = readParse (BASE_URL );

JSONArray jarray = new JSONArray (new String (data ));
String studentInfo = "";
For (int I = 0; I <jarray. length (); I ++)
{
JSONObject item = jarray. getJSONObject (I );
String name = item. getString ("description ");
StudentInfo + = "Description :";
StudentInfo + = name;
String address = item. getString ("img ");
StudentInfo + = "img :";
StudentInfo + = address;
Int age = item. getInt ("link ");
StudentInfo + = "link :";
StudentInfo + = age;
StudentInfo + = "\ n ";
}
MStudentTextView. setText (studentInfo );

}
 
Private byte [] readParse (String baseUrl) throws IOException {
// TODO Auto-generated method stub
ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
Byte [] data = new byte [1024];
Int len = 0;

Try {
URL url = new URL (baseUrl );
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
InputStream inStream = conn. getInputStream ();
While (len = inStream. read (data ))! =-1 ){
OutStream. write (data, 0, len );
}
InStream. close ();
Return outStream. toByteArray ();
} Catch (MalformedURLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

Return null;
}


}

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.