JSON data type

Source: Internet
Author: User
Tags object object

Java is divided into two types of data, one is the basic data type, one is the reference data type;

Basic data types include Byte/short/int/long (4 integers), float/double (2 floating-point types), char (1-character), Boolean (1-Boolean)

Reference data types: Class classes, interface interface, arrays array

There are two types of JSON data: Jsonobject and Jsonarray

  • Jsonobject: {key:value,key:value}, where key is a string type, value is an object type, elements are separated by commas, inner key and value are separated by colons, and the outermost is curly braces
    • New Jsonobject:jsonobject obj = new Jsonobject ();
    • Add elements to the Jsonobject object: Obj.put (String key, Object object);
    • Jsonobject Type Conversions
      • From Jsonobjet to String:obj.toString ();
      • From string to JSONObject:JSONObject.parseObject (str);
    • Gets the value of the element from the Jsonobject object: Obj.getter (String key);
  • jsonarray:[element 1, Element 2, Element 3 ...], the outermost side is square brackets
    • New Jsonarray object: Jsonarray arr = new Jsonarray ();
    • Convert the String type to jsonarray:string str = "[{\" name\ ": \" Zhang San \ "},1234,12.34]"; Jsonarray arr = Jsonarray.parsearray (str);
    • Add an element to the Jsonarray object: Array.add (object e);
    • Jsonarray Type Conversions
      • The Jsonarray type is converted to String type: arr.tostring ();
      • The string type is converted to Jsonarray type: Jsonarray.parsearray (string str);
    • Extracts the value of an element from a Jsonarray object: array.getter (int index);
    • Traverse Jsonarray:
      • Method One:
        1  for (int i = 0;i < Arr.size (); i++) {2    System.out.println (Arr.get (i));         3 }
      • Method Two: Enhance the For loop
        1  for (Object item:arr) {2    System.out.println (item); 3 }

Reprint please indicate the source

JSON data type

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.