String to the Java object, the way to map

Source: Internet
Author: User


1. Use Fastjson to convert string to map:


String out;

Object succesresponse = Json.parse (out); Convert to Object first

Map map = (map) succesresponse; Object strong convert to map


2. String to Java Object

Fastjson to convert a string string to a Java object or an array of objects

The code is as follows

Import java.util.ArrayList;  
Import Java.util.Arrays;  
  
Import java.util.List;  
Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONObject;  
  
  
Import com.alibaba.fastjson.TypeReference; public class Testfastjson {public static void main (string[] args) {//Convert to Object String JSON    
        string = "{\" a\ ": 51,\" B\ ": 0}";  
        Usa u1 = Json.parseobject (jsonstring, New typereference<usa> () {});  
        Usa U2 = Json.parseobject (Jsonstring,usa.class);    
        Convert to object array String jsonstring2 = "[{\ A\": 51,\ "B\": 0}] ";    
        usa[] Usa2 = Json.parseobject (jsonstring2, New typereference<usa[]> () {}); 
        List List = Arrays.aslist (USA2); Convert to ArrayList arraylist<usa> list2 = Json.parseobject (jsonstring2, New TYPEREFERENCE&LT;ARRAYLIST&LT;USA&G 
        
        T;> () {}); Convert to ArrayList (default) List3 the same as List4 effect arraylist<jsonobject> list3 = Json.parseobject (jsonstring2, New Arraylist<usa> (). GetClass ()); 
        arraylist<jsonobject> list4 = Json.parseobject (jsonstring2, Arraylist.class);
        	for (int i = 0; i < list4.size (); i++) {//recommend this jsonobject IO = list4.get (i);
		System.out.println (Io.get ("a") + "======adn=====" +io.get ("B"));
	Class Usa {private int count = 1888;
	Private String base = "Project";    
    Private Long A;    
    Public Long Geta () {return A;    
    public void SetA (Long a) {this.a = A;    
    Private String B;    
    Public String Getb () {return b;    
    public void Setb (String b) {this.b = b;    
  }    
}

The JSON string is converted directly to the list

or convert to an array of objects

usa[] Usa2 = Json.parseobject (jsonstring2, New typereference<usa[]> () {});

Object array Goto List

List List = Arrays.aslist (USA2);

When we use new typereference, we generate multiple class files. How many new typereference will be added to class

Even if we write new typereference in the For Loop (0-n), the code generates a class file, Fastjson How many new typereference we've written, not how many times we've called New typereference. Recommended Use Arraylist.class

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.