Reading and Writing json in android

Source: Internet
Author: User

Reading and Writing json in android

Reading json in android

1. Read the Json content in the json File

First, put the json file under the directory assets:

Test. json

 

{programmers:[{firstName:Brett,lastName:McLaughlin,email:aaaa},{firstName:Jason,lastName:Hunter,email:bbbb},{firstName:Elliotte,lastName:Harold,email:cccc}],authors:[{firstName:Isaac,lastName:Asimov,genre:sciencefiction},{firstName:Tad,lastName:Williams,genre:fantasy},{firstName:Frank,lastName:Peretti,genre:christianfiction}],musicians:[{firstName:Eric,lastName:Clapton,instrument:guitar},{firstName:Sergei,lastName:Rachmaninoff,instrument:piano}],cat:test}

Read from main. java

 

 

Try {InputStreamReader isr = new InputStreamReader (getAssets (). open (test. json), UTF-8); // open test. json BufferedReader br = new BufferedReader (isr); // read isr String line; StringBuilder builder = new StringBuilder (); while (line = br. readLine ())! = Null) {builder. append (line);} br. close (); isr. close (); JSONObject root = new JSONObject (builder. toString (); System. out. println (root. getString (cat); JSONArray arry = root. getJSONArray (programmers); for (int I = 0; I display result:

 

To read the given Json data:

Values can be directly attached.

 

// JSONObject root = new JSONObject (builder. toString (); Replace with-> JSONObject root = new JSONObject ({'grammers ': [{'firstname': 'brett', 'lastname': 'mclaughlin ', 'email ': 'aaa'}]});
2. Create data in JSON format
 
JSONObject root1=new JSONObject(); try { root1.put(cat,it); JSONObject lan1=new JSONObject(); lan1.put(id,1); lan1.put(ide,android); lan1.put(name,java); JSONObject lan2=new JSONObject(); lan1.put(id,2); lan1.put(ide,xcod); lan1.put(name,ios); JSONObject lan3=new JSONObject(); lan1.put(id,3); lan1.put(ide,vs); lan1.put(name,C#); JSONArray array=new JSONArray(); array.put(lan1); array.put(lan2); array.put(lan3); root1.put(programmers,array); System.out.println(root1.toString()); } catch (JSONException e) { e.printStackTrace(); } } 


Display result:
 
  

 

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.