Java JSON string jsonobject and Jsonarray and values

Source: Internet
Author: User

Import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsontest {public static void main (string[] args {String Jostr = "{name:\" Zhang San \ ", age:\" 20\ "}";//Convert the JSON string to Jsonobjectjsonobject Jsonobject = Jsonobject.fromobject ( JOSTR);//GetString ("") to remove the information inside the string name = Jsonobject.getstring ("name"); String age = jsonobject.getstring ("Age");//Output Zhang San 20system.out.println (name+ "" +age); String jastr = "[{user:{name:\" Zhang San \ ", age:\" 20\ "}},{score:{yuwen:\" 80\ ", Shuxue:\" 90\ "}}]";// Convert the Jsonarray string to Jsonarrayjsonarray Jsonarray = Jsonarray.fromobject (JASTR);//Remove the first element of the array jsonobject Juser = Jsonarray.getjsonobject (0). Getjsonobject ("user");//Remove the information for the first element and convert it to jsonobjectstring name2 = juser.getstring ("Name "); String age2 = juser.getstring ("Age");//Output Zhang San 20system.out.println (name2+ "" +age2);//Take out the second element of the array, and converted to Jsonobjectjsonobject Jscore = Jsonarray.getjsonobject (1). Getjsonobject ("score");//Take out the information of the second element string Yuwen = Jscore.getstring ("Yuwen"); String Shuxue = jscore.getstring ("Shuxue");//Output 90SYstem.out.println (yuwen+ "" +shuxue);} 

Java JSON string jsonobject and Jsonarray and values

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.