Java parsing json file method (i)

Source: Internet
Author: User
Tags readfile

First, first, you need to import the relevant jar packages in the Eclipse project , jar package See link: Http://yunpan.alibaba-inc.com/share/link/NdA5b6IFK

Two provide a JSON file to parse, Apkinfo.json is as follows: [{"Name": "Account", "package_name": "Com.android.account", "check_version": "1.2"},{"name": "System Upgrade", " Package_name ":" Com.android.osupdate "," check_version ":" 1.0 "},] Iii. Java code consists of two classes, one is the Util class to read the contents of the file, and the other is the main program test class。 (1) Util.java:Import Java.io.bufferedreader;import Java.io.fileinputstream;import Java.io.ioexception;import  Java.io.InputStreamReader; public class Util {public string ReadFile (string Path) {BufferedReader reader = null; String laststr = ""; Try{fileinputstream FileInputStream = new FileInputStream (Path); InputStreamReader InputStreamReader = new InputStreamReader (FileInputStream, "UTF-8"); reader = new BufferedReader (InputStreamReader); String tempstring = Null;while ((tempstring = Reader.readline ()) = null) {laststr + = tempstring;} Reader.close ();} catch (IOException e) {e.printstacktrace ();} FINALLY{IF (reader! = null) {try {reader.close ();} catch (IOException e) {e.printstacktrace ()}}} return LASTSTR;} } (2) Test.java:Import Java.util.arraylist;import Java.util.iterator;import java.util.List; Import Net.sf.json.jsonarray;import Net.sf.json.JSONObject; public class Test {public static void main (string[] args) {//TODO auto-generated method stubstring Jsoncontext = new Uti L (). ReadFile ("D:\\test\\apkinfo.json"); Jsonarray Jsonarray = Jsonarray.fromobject (jsoncontext); int size = Jsonarray.size (); SYSTEM.OUT.PRINTLN ("Size:" + size); for (int i = 0; i < size; i++) {Jsonobject jsonobject = Jsonarray.getjsonobject (i); System.out.println ("[" + i + "]name=" + jsonobject.get ("name")); System.out.println ("[" + i + "]package_name=" + jsonobject.get ("package_name")); System.out.println ("[" + i + "]check_version=" + jsonobject.get ("check_version"));}}} Iv. The following results have been implemented:Size:2[0]name= account [0]package_name=com.android.account[0]check_version=1.2[1]name= system upgrade [1]package_name= com.android.osupdate[1]check_version=1.0

How Java parses a JSON file (i)

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.