JSON File Parsing

Source: Internet
Author: User

#Json.txt

{

Name: "China", provinces: [{name: "Heilongjiang", citys: ["Jiamusi", "Daqing", "Harbin", "Qiqihar ", "Mudanjiang"] },{ name: "Liaoning", citys: ["Shenyang", "Dalian", "Panjin"] },{ name: "Jilin", citys: ["Jilin City", "Changchun City", "Siping City"]} # jsonparser. javaimport Java. io. *; import Java. text. parseexception; import Org. JSON. *;/*** read data from a JSON file to form a JSON object. Read the object data and print */public class jsonparser {public static void main (string [] ARGs) {// read data from the JSON file stringbuffer = new stringbuffer (); stri NG line = NULL; try {bufferedreader BR = new bufferedreader (New filereader (new file ("json.txt"); While (line = Br. Readline ())! = NULL) {stringbuffer. append (line) ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} Try {// form the JSON file data to the jsonobject = new jsonobject (stringbuffer. tostring (); // obtain jsonobject data and print jsonarray provinces = jsonobject. getjsonarray ("provinces"); string name = NULL; stringbuffer jsonfileinfo = new stringbuffer (); jsonarray citys = NULL; For (INT I = 0; I <provinces. length (); I ++) {name = provinces. getjsonobject (I ). getstring ("name"); jsonfileinfo. append ("/nname:" + name + "/N" + "citys:"); citys = provinces. getjsonobject (I ). getjsonarray ("citys"); For (Int J = 0; j <Citys. length (); j ++) {jsonfileinfo. append (Citys. getstring (j) + "/t") ;}} system. out. println (jsonfileinfo);} catch (parseexception e) {e. printstacktrace () ;}} running result: Name: Heilongjiang citys: Jiamusi Daqing Harbin Qiqihar Mudanjiang name: Liaoning citys: Shenyang Dalian Panjin name: Jilin citys: Siping City, Changchun City, Jilin City

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.