JSON Parse string J Simple Instance

Source: Internet
Author: User
Tags parse string

JSON parses a string instance, parsing content as:

{"Info": [{"Code": "C", "Key": "028", "nearest": "No", "value": "???"},{"Code": "N", "Key": "0771", "nearest": "No", "value" : "????"},{"code": "L", "Key": "0772", "nearest": "No", "value": "????"},{"code": "G", "Key": "0773", "nearest": "No", " Value ":"???? "}]," ResultCode ":" 0 "," resultinfo ":" SUCCESS "}

URL: http://www.bejson.com/go.html?u=http://www.bejson.com/jsonview2/(JSON view)

First step: Look at the jsons view, from the innermost layer to write

One thing to emphasize here is that one must correspond to each other to resolve

The first layer of code:

Package Com.example.com.scxh.jsonjiexi;

Name is Citybean
public class Citybean {
Private String Code;
Private String key;
Private String nearest;
private String value;


Public String GetCode () {
return code;
}


public void Setcode (String code) {
This.code = code;
}


Public String GetKey () {
Return key;
}


public void Setkey (String key) {
This.key = key;
}


Public String getnearest () {
return nearest;
}


public void Setnearest (String nearest) {
This.nearest = nearest;
}


Public String Getlvalue () {
return value;
}


public void Setlvalue (String lvalue) {
This.value = Lvalue;
}


}

Second layer: From the inside to the second level of the outer number

Package Com.example.com.scxh.jsonjiexi;


Import java.io.Serializable;
Import java.util.List;


Import junit.runner.Version;


Import org.apache.http.entity.SerializableEntity;


public class Updatedao implements serializable{
Private list<citybean> info;
Private String Resultinfo;
Private String ResultCode;
Public list<citybean> GetInfo () {
return info;
}
public void SetInfo (list<citybean> info) {
This.info = info;
}
Public String Getresultinfo () {
return resultinfo;
}
public void Setresultinfo (String resultinfo) {
This.resultinfo = Resultinfo;
}
Public String Getresultcode () {
return resultcode;
}
public void Setresultcode (String resultcode) {
This.resultcode = ResultCode;
}

}

Step three: Parsing in the main activty (here I also wrote the adapter in the main activty)

Package Com.example.com.scxh.jsonjiexi;


Import java.util.ArrayList;
Import java.util.List;


Import Com.alibaba.fastjson.JSON;
Import Com.google.gson.Gson;


Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ListView;
Import Android.widget.TextView;


public class Mainactivity extends Activity {
Private ListView Mllistview;
Private Mybaseadapter Mmadapter;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mllistview = (ListView) Findviewbyid (r.id.listviews);
Read the contents of a folder assets (local content)
String JSON = Readassetsfile.readtxt (This, "citymessage.txt");
LOG.E ("JSON", JSON);
// Updatedao user = Json.parseobject (JSON, Updatedao.class);


Gson gson=new Gson ();


Updatedao User=gson.fromjson (JSON, updatedao.class);


list<citybean> list = User.getinfo ();
Mmadapter = new Mybaseadapter (this);
Mllistview.setadapter (Mmadapter);
Mmadapter.setdata (list);
}


public class Mybaseadapter extends Baseadapter {
Private list<citybean> mlist = new arraylist<citybean> ();
Private Layoutinflater Minflater;
Private Context Mcontext;


Public Mybaseadapter (Context context) {
Mcontext = context;
Minflater = Layoutinflater.from (Mcontext);
}


Public Mybaseadapter (context context, list<citybean> List) {
Mcontext = context;


Minflater = Layoutinflater.from (Mcontext);
mlist = list;


}


public void SetData (list<citybean> List) {
mlist = list;
Notifydatasetchanged ();
}


@Override
public int GetCount () {
return Mlist.size ();
}


@Override
Public Object getItem (int position) {
return Mlist.get (position);
}


@Override
public long getitemid (int position) {
return position;
}


@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Viewholder Holder;
if (Convertview = = null) {
Holder = new Viewholder ();
Convertview = minflater.inflate (R.layout.listview, NULL);


Holder.titletxt = (TextView) convertview
. Findviewbyid (R.id.textviews);
Holder.infotxt = (TextView) convertview
. Findviewbyid (R.ID.TEXTVIEWSS);
Convertview.settag (holder);
} else {
Holder = (viewholder) convertview.gettag ();
}


Citybean msg = (Citybean) getItem (position);
;
Holder.titleTxt.setText (Msg.getcode ());
Holder.infoTxt.setText (Msg.getkey ());
return convertview;
}

Class Viewholder {
TextView titletxt = null;
TextView infotxt = null;
}


}
}

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Here to read the file (stream read) in a separate package of Chengde a class

Package Com.example.com.scxh.jsonjiexi;


Import java.io.IOException;
Import Java.io.InputStream;


Import Android.content.Context;
The wrapper class is used for reading in
public class Readassetsfile {
private static String text;


public static string Readtxt (context context, string name) {
try {
InputStream is = Context.getassets (). open (name);
int size = is.available ();
Read the entire asset into a local byte buffer.
byte[] buffer = new Byte[size];
Is.read (buffer);
Is.close ();
Convert the buffer into a string.
Text = new String (buffer, "utf-8");
} catch (IOException e) {
E.printstacktrace ();
}
return text;
}
}

--------------------------------------------------------------------------------------------------------------- ---------------------------------

JSON Parse string J Simple Instance

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.