Android learning-JSON data parsing

Source: Internet
Author: User
Tags tagname

In Android Application Development, common data exchange formats include XML and JSON. Each of these two methods has its own advantages. We can choose a suitable one in specific application development. Next let's take a look at josn data parsing:

The example is always the best tutorial. Let's take a look at the example below!

There is a JSON data: "{" username ":" zhangsan "," password ":" 123456 "}"

After parsing, the corresponding data is displayed in the corresponding control:

This is the above effect.

Using JSON in Android requires a jar package, gson-1.7.jar; can be downloaded on Google's website. Add this package to the build path of the project.

The following is the source code of this project (for the classes and methods in the source code, refer to the API documentation ):

Androidmanifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

Package = "com. gufengxiachen"

Android: versioncode = "1"

Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK Android: minsdkversion = "8"/>

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">

<Activity Android: Name = ". JSON"

Android: Label = "@ string/app_name">

<Intent-filter>

<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>

</Intent-filter>

</Activity>

</Application>

</Manifest>

Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

<Textview

 

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/username"

/>

<Edittext

Android: Id = "@ + ID/username"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

> </Edittext>

<Textview

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/password"

/>

<Edittext

Android: Id = "@ + ID/password"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

> </Edittext>

<Button

Android: Id = "@ + ID/parse"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/parse"

>

</Button>

</Linearlayout>

JSON. Java:

Package com. gufengxiachen;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. View. view;

Import Android. View. View. onclicklistener;

Import Android. widget. Button;

Import Android. widget. edittext;

Public class JSON extends activity {

/** Called when the activity is first created .*/

 

Private string name;

Private string ages;

 

Public String getname (){

Return name;

}

Public void setname (string name ){

This. Name = Name;

}

Public String getages (){

Return ages;

}

Public void setages (string ages ){

This. Ages = ages;

}

Private edittext username = NULL;

Private edittext Password = NULL;

Private button parse = NULL;

 

Private string jsondata = "[{\" USERNAME \ ": \" zhagnsan \ ", \" password \ ": \" 123456 \ "}]";

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Username = (edittext) findviewbyid (R. Id. username );

Password = (edittext) findviewbyid (R. Id. Password );

Parse = (button) findviewbyid (R. Id. PARSE );

Parse. setonclicklistener (New parselistener ());

}

Public class parselistener implements onclicklistener {

@ Override

Public void onclick (view v ){

// Todo auto-generated method stub

Parsejson = new parsejson ();

JSON = parsejson. parse (jsondata );

Username. settext (JSON. getname ());

Password. settext (JSON. getages ());

}

}

}

Parsejson. Java:

Package com. gufengxiachen;

Import java. Io. ioexception;

Import java. Io. stringreader;

Import com. Google. gson. Stream. jsonreader;

Public class parsejson {

Public JSON parse (string jsondata ){

JSON = new JSON ();

Jsonreader = new jsonreader (New stringreader (jsondata ));

Try {

Jsonreader. beginarray ();

While (jsonreader. hasnext ()){

Jsonreader. beginobject ();

While (jsonreader. hasnext ()){

String tagname = jsonreader. nextname ();

If (tagname. Equals ("username ")){

JSON. setname (jsonreader. nextstring ());

System. Out. println (JSON. getname ());

} Else if (tagname. Equals ("password ")){

JSON. setages ("" + jsonreader. nextint ());

System. Out. println (JSON. getages ());

}

}

Jsonreader. endobject ();

}

Jsonreader. endarray ();

} Catch (ioexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

}

Return JSON;

}

}

Related Article

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.