JSON string converted to Java object

Source: Internet
Author: User

1. The JSON string is converted to Java Object 1.1. JSON string converted to Java bean

Json2bean.java

Package Jackson;

import java.io.IOException;

import Org.codehaus.jackson.map.ObjectMapper;

Public class Json2bean {

Public static void main (string[] args)throws ioexception{

String Jsonstr = "{\" name\ ": \" Morris\ ", \" age\ ": \" 22\ ", \" sex\ ": \ " male \ "}";

Objectmapper mapper = new objectmapper ();

Person person = mapper.readvalue (jsonstr, person. Class);

System. out. println (person);

}

}

1.2. JSON string conversion to Java list collection

Json2list.java

Package Jackson;

import java.io.IOException;

import Java.util.LinkedHashMap;

import java.util.List;

import Org.codehaus.jackson.map.ObjectMapper;

Public class json2list {

Public static void main (string[] args)throws IOException {

String jsonstr = "[{\" name\ ": \" bob\ ", \" age\ ": \" 33\ ", \" sex\ ": \ " F "},"

+ "{\" name\ ": \" Morris\ ", \" age\ ": \" 22\ ", \" sex\ ": \" male \ "}]";

Objectmapper mapper = new objectmapper ();

list<linkedhashmap<string, string>> list = mapper.readvalue (jsonstr, list. class );

for (linkedhashmap<string, string> linkedhashmap:list) {

for (String Key:linkedHashMap.keySet ()) {

System. out. println (key+":"+linkedhashmap.get (key));

}

}

}

}

The results of the operation are as follows:

Name:bob

Age:33

Sex: Female

Name:morris

Age:22

Sex: Male

1.3. JSON string conversion to Java map Collection

Json2map.java

Package Jackson;

import java.io.IOException;

import Java.util.Map;

import Java.util.Set;

import Org.codehaus.jackson.map.ObjectMapper;

Public class Json2map {

Public static void main (string[] args)throws IOException {

String Jsonstr = "{\" success\ ": True,\" a\ ": {\" address\ ": \" address2\ ", \" name\ ": \" haha2\ ", \" id\ ": 2,\" email\ ": \" Email2\ "},"+

"\" b\ ": {\" address\ ": \" Address\ ", \" name\ ": \" Haha\ ", \" id\ ": 1,\" email\ ": \" Email\ "}}";

Objectmapper mapper = new objectmapper ();

map map = mapper.readvalue (jsonstr, map. Class);

Set keySet = Map.keyset ();

for (Object Key:keyset) {

System. out. println (key+":"+map.get (key));

}

}

}

The results of the operation are as follows:

Success:true

A:{address=address2, NAME=HAHA2, ID=2,EMAIL=EMAIL2}

B:{address=address, Name=haha, Id=1,email=email}

1.4. JSON string conversions to arrays

Json2array.java

Package Jackson;

import java.io.IOException;

import Org.codehaus.jackson.map.ObjectMapper;

Public class Json2array {

Public static void main (string[] args)throws IOException {

String jsonstr = "[{\" name\ ": \" bob\ ", \" age\ ": \" 33\ ", \" sex\ ": \ " F "},"

+ "{\" name\ ": \" Morris\ ", \" age\ ": \" 22\ ", \" sex\ ": \" male \ "}]";

Objectmapper mapper = new objectmapper ();

person[] persons = Mapper.readvalue (Jsonstr, person[]. Class);

for (Person Person:persons) {

System. out. println (person);

}

}

}

The results of the operation are as follows:

person [Name=bob, age=33, sex= women]

person [Name=morris, age=22, sex= male]

JSON string converted to Java object

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.