Android Read and write JSON-formatted data jsonwriter and Jsonreader

Source: Internet
Author: User

The last few months have not done Android programming, gradually forget something. Recently, I'm looking for an Android job to keep picking up the old stuff. The company has no network since the beginning of the first move, until today the company has network access, the departments only began to work. Do not say these, the following begins today's body.

JSON -formatted data is typically used for network transmissions, especially on mobile devices that interact with the server's data. There are obvious advantages to JSON compared to XML . Data in XML format is very bloated, and passing a small amount of data comes with a lot of additional format data. and JSON , in addition to a small number of symbols, are all real data, so there are more and more programs with JSON to replace XML. Data in JSON format is not only widely used in network transmission, but also is often applied to local storage.

Data in the

json format can store arrays and objects. Array with a pair of brackets ([...]) Represents the object with a pair of curly braces Said. Arrays and objects can be nested, for example, the following is an array of objects represented by the json :
[{id:1,name:" Android General "},{id:2,name:" Android General "}]

The classes used by the Android SDK to read and write JSON -formatted data are Jsonreader and jsonwriter. These two classes can handle arrays and objects, and the code that handles arrays must be placed between the Beginarray and Endarray methods, and the code that handles the objects must be placed in BeginObject and between the EndObject.

Here is a read and write JSON file case, the main activity code is as follows:

Package Com.gc.androidjsonpractice;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Android.os.bundle;import Android.os.environment;import Android.annotation.suppresslint;import Android.app.activity;import Android.graphics.paint.join;import Android.util.jsonreader;import Android.util.jsonwriter;import android.widget.toast;/** * Function: Review and practice JSON using a * * * @author Android General * */@SuppressLint ("Newapi") public class Mainactivity E Xtends Activity {private String filepath;private fileoutputstream fileoutputstream;private FileInputStream FileInputStream; @SuppressLint ({"Newapi", "Sdcardpath"}) @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//json file storage Path filepath = Environment.getexternalstoragedirectory (). toString () + "/test.json"; System.out.println ("FilePath:" + filePath);//view actual path try {fileoutputstream = new FileOutputStream (fiLepath); @SuppressWarnings ("resource")//start writing JSON data jsonwriter jsonwriter = new Jsonwriter (New OutputStreamWriter ( FileOutputStream, "UTF-8")); Jsonwriter.beginobject (); Jsonwriter.name ("id"). Value ("1"); Jsonwriter.name ("name"). Value ("Android General"); Jsonwriter.endobject (); SYSTEM.OUT.PRINTLN ("JSON data is written! Jsonwriter.close ();//start reading JSON data System.out.println ("Start reading JSON data") FileInputStream = new FileInputStream (FilePath) ; @SuppressWarnings ("resource") jsonreader Jsonreader = new Jsonreader (new InputStreamReader (FileInputStream, "UTF-8") ); Jsonreader.beginobject (); System.out.println ("haha:" +jsonreader.tostring ()); while (Jsonreader.hasnext ()) {if (Jsonreader.nextname (). Equals (" ID ")) {SYSTEM.OUT.PRINTLN (" ID: "+ jsonreader.nextstring ());} if (Jsonreader.nextname (). Equals ("name")) {System.out.println ("name:" + jsonreader.nextstring ());}} Jsonreader.endobject (); Jsonreader.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


The results of the program run as follows:

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/38730207

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.