JSON to Java Object

Source: Internet
Author: User
Tags string format

After using the platform and rarely touch the Java and JS of the underlying code, a few days ago remote to help a new remote debugging code, this Meng new according to the online tutorial to the JSON word representable Java object has been error. The truth is its JSON string format is not correct, his is an array, but written an object, although the use of JSON online check is correct, but the meaning of the representative is not the same ~ ~ ~ (here cover face ~ laughter ~ haha) so understand how to convert before you must be familiar with the format of JSON, The reason why I write a blog is to hope that I do not forget the past, do not want their own more formal, only this is a diary, and occasionally see themselves can also be a lot of exchanges with others.

So what are the conversion methods for JSON and Java? What I know is that there are three kinds of:

The first method, use Json-lib.

The second way, use JACKSON.

The third method uses Gson.

The most I have used is the conversion of Gson, which basically does not depend on other packages. The use of natural is very cool, the value of the way is very flexible. The complex JSON value is basically done. There are two kinds of concepts in Gson. One is Jsonobject and Jsonarray.

1. Convert the bean to a JSON string:

 Public Static String beantojsonstring (Object Bean) {    returnnew  Gson (). ToJson (bean);}

2. Convert the JSON string to an object: ( of course, you need to build the Pojo class in advance )

 Public Static Object jsontoobject (String json,class beanclass) {    new  Gson ();     = Gson.fromjson (JSON, beanclass);     return Res;}

Note: The converted object is strongly forwarded before use: For example, bean bean1 = (bean) formatutil.jsontoobject (JSON, bean.class);

3. Convert the JSON string to a Jsonobject object:

New Jsonparser (). Parse (JSONSTR). Getasjsonobject ();

4. Parse the JSON string of the Jsonarray type into an object method:

Newnew= parser.parse (jstring). Getasjsonarray (); ArrayListNew arraylist<channelsearchenum>for(jsonelement Obj:jarray) {     = Gson.fromjson (obj, channelsearchenum.  Class);    Lcs.add (CSE);  }

Or

String json = "[{\" username\ ": \" test\ "},{\" username\ ": \" Test2\ "}]"; System.out.println (new Jsonparser (). Parse (JSON). Getasjsonarray (). Get (0). Getasjsonobject (). Get (" Username "). getasstring ());

5. Get the Jsonarray object that corresponds to the specified key value in Jsonobject:

String json= "{\" pids\ ": [\" 1\ ", \" 2\ ", \" 3\ "]}"; System.out.println (new Jsonparser (). Parse (JSON). Getasjsonobject (). Getasjsonarray ("PIDs"). Get (0). Getasstring ());

The second way to achieve the conversion with Jackson, you can refer to this blog: http://blog.csdn.net/a123demi/article/details/43112547

The third Kind uses json-lib, this is the method which the new hour uses, may refer to this blog: http://blog.csdn.net/chenbin520/article/details/8776915, but I believe eventually you will also choose to use Gson, Because see so many packages, the method is so cumbersome, but this is completely personal immature small advice, everyone has the habit of each person, the way to achieve the function of thousands, some people like simple, some people like dazzle skills. I think it's better than a few more lines of comment, think about how can not write duplicate code, write let people read and understand code ~ Vaguely remember that year a friend to dazzle technology forcibly use reflection to get the object of the method, the loop gets the object attribute array, the final code view is found, It was not long before the senior man did not know whether he had resigned because of the matter. Ah ~ again opened the Spit Groove mode ~ To see the time of the Sea Thief King ~~~~~ a day not more Bo body blue thin, I also want to spit trough a bit of their own ~ Spicy chicken yo

JSON to Java Object

Related Article

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.