Without further ado, I will post the code to you first. The specific code is as follows:
import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.util.*; public class JavaTest { public static void main(String[] args){ JSONObject obj=new JSONObject(); obj.put("derek","23"); obj.put("dad", "49"); obj.put("mom", "45"); System.out.println("JSONObject object created through a constructor: "+obj); Map<string,string> map=new LinkedHashMap<>(); map.put("derek","23"); map.put("dad", "49"); map.put("mom", "45"); System.out.println("Convert the map object to a JSONObject object through the fromObject method: "+JSONObject.fromObject(map)); JSONArray arr=new JSONArray(); arr.add(0,"derek"); arr.add(1,"dad"); arr.add(2,"mom"); System.out.println("JSONArray created through the constructor: "+arr); ArrayList list=new ArrayList<>(); list.add("derek"); list.add("dad"); list.add("mom"); System.out.println("Convert an Arraylist object to a JSONArray object through the fromObject method: "+JSONArray.fromObject(list)); System.out.println("Convert HashMap object to JSONArray object through fromObject method"+JSONArray.fromObject(map)); String str="{/"derek/":23,/"dad/":49,/"mom/":45}"; System.out.println("The parsed JSON object: "+JSONObject.fromObject(str)); //Transfer the output Iterator it=obj.keys(); while(it.hasNext()){ String key=it.next(); System.out.println(key+":"+obj.get(key)); } } }The operation results are as follows:
JSONObject object created through constructor: {"derek":"23","dad":"49","mom":"45"}
Convert map object to JSONObject object through fromObject method: {"derek":"23","dad":"49","mom":"45"}
JSONArray created through constructor: ["derek","dad","mom"]
Convert Arraylist object to JSONArray object through fromObject method: ["derek","dad","mom"]
Convert HashMap object to JSONArray object through fromObject method[{"derek":"23","dad":"49","mom":"45"}]
The parsed JSON object: {"derek":23,"dad":49,"mom":45}
derek:23
dad:49
mom:45
Java List is a collection interface. As long as it is a collection class interface, it will have an "iterator". Using this iterator, you can operate on a set of objects in the list memory. If you want to operate this list memory, you must first get an instance of this iterator: Iterator it=l.iterator();
Use the add() method to add a new member object. All it can add is an object, not a basic data type. The container also corresponds to the get() and remove() methods to get and delete data members.
Example 1.
import java.util.*; public class ArrayListTest{ public static void main(String dd[]){ //new a storage list List l=new ArrayList(); //Because the Collection framework can only store objects, new encapsulation class l.add(new Integer(1)); l.add(new Integer(2)); l.add(new Integer(3)); l.add(new Integer(4)); Iterator it=l.iterator(); //hasNext is the value that takes the current value. Its calculation process is to determine whether the next value has a value if it continues. while(it.hasNext()){ //Suppose it.next encapsulation class, call Integer's intValue method to get the return value as int and assign it to i; int i=((Integer)it.next()).intValue(); System.out.println("Element in list is : "+i);}}}What is the difference between ArrayList list = new ArrayList() and List<String> list = new ArrayList<String>()? ?
1. The difference between storing content
ArrayList can store any type of item
List<type> can only store items of the specified type
2. Is it easy to use
List<>Easy to use than ArrayList
Because when using the value inside ArrayList, it must be cast
I believe that those who have used ArrayList have a deep understanding, because the values stored in ArrayList have been converted to Object type
3. Time of occurrence
ArrayList appears earlier than List<>
List<> appears in C# 2.0 and is one of its most powerful features
4. The difference between the namespace
The namespace of ArrayList is System.Collections
The namespace of List<> is System.Collections.Generic
Generic means generic, using List<> means using generic technology
5. Frequency of use in programming
In most cases, ArrayList can be abandoned, after all, it is a technology before C# 2.0.
This can be seen from creating a new class in Visual Studio
When creating a new class, the default namespace is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Obviously there is no System.Collections, because the use of ArrayList is indeed inconvenient
The above content is the knowledge of using JSONObject and JSONArray in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support to the Wulin Network website!