Generate and use JSON data instance _android in Android

Source: Internet
Author: User

1, the production of JSON

Copy Code code as follows:

Package com.example.usingjson2;

Import org.json.JSONException;
Import Org.json.JSONObject;

Import Android.os.Bundle;
Import android.app.Activity;

public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

System.out.println ("\" Hello world\ "");
Jsonobject object = new Jsonobject ();

try {
Object.put ("One", "Yu");
Object.put ("Two", "XI");
Object.put ("Three", "Kuo");
Object.put ("Four", "HI");
System.out.println (object);

catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
}

2, the use of JSON

(1) First create a new class in the current package

Copy Code code as follows:

Package Com.example.useingjson;

public class Data {

public static final String jsonstr = "{' arr ': [1,2,3,4], ' dat ': [Yu,xi,kuo]}";
}

(2) calling in the activity

Copy Code code as follows:

Package Com.example.useingjson;

Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;

Import Android.os.Bundle;
Import android.app.Activity;

public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

System.out.println (DATA.JSONSTR);

try {
Jsonobject jsonobject = new Jsonobject (DATA.JSONSTR);
Jsonarray Jsonarray = Jsonobject.getjsonarray ("arr");
System.out.println (Jsonarray);

for (int i = 0; i < jsonarray.length (); i++)
{
System.out.println (Jsonarray.get (i));
}

Jsonarray jsonArray2 = Jsonobject.getjsonarray ("Dat");
System.out.println (JsonArray2);
for int j = 0; J < Jsonarray2.length (); j + +)
{
System.out.println (Jsonarray2.get (j));
}

catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

3. Note

The production and use of JSON is two projects, because just contact Android, the code is relatively simple, emphasis on understanding the principle.

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.