Use of Gson Jsonparser

Source: Internet
Author: User

 Packageiotest;ImportCom.google.gson.Gson;ImportCom.google.gson.JsonArray;Importcom.google.gson.JsonElement;ImportCom.google.gson.JsonObject;ImportCom.google.gson.JsonParser;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;/** * @authorYan *@versionV1.0 * @desc*/ Public classIotest { Public Static voidMain (string[] args) {result result=NewResult (); Result.setno (1); Result.setmsg ("OK"); List<String> list =NewArraylist<string>(); List.add ("123"); List.add ("234"); List.add ("345");                Result.setobj (list); Gson Gson=NewGson (); String JSON=Gson.tojson (Result);                SYSTEM.OUT.PRINTLN (JSON); /*** {"No": 1, "MSG": "OK", "obj": ["123", "234", "345"]}*/Jsonparser JP=NewJsonparser (); Jsonobject Jsonobj=Jp.parse (JSON). Getasjsonobject (); intNo = Jsonobj.get ("No"). Getasint (); System.out.println ("No:" +no); String msg= Jsonobj.get ("msg")). getasstring (); System.out.println ("Msg:" +msg); Jsonarray Jsonarray= Jsonobj.get ("obj"). Getasjsonarray (); Iterator<JsonElement> iter =Jsonarray.iterator (); List<String> OBJS =NewArraylist<string>();  while(Iter.hasnext ()) {Objs.add (Iter.next (). getasstring ()); }                 for(String s:objs) {System.out.println ("==s:" +s); }            }            Static classresult{PrivateInteger No; PrivateString msg; PrivateObject obj;  PublicInteger Getno () {returnNo; }         Public voidSetno (Integer no) { This. No =No; }         PublicString getmsg () {returnmsg; }         Public voidsetmsg (String msg) { This. msg =msg; }         PublicObject getobj () {returnobj; }         Public voidsetobj (Object obj) { This. obj =obj; }                    }}

Use of Gson Jsonparser

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.