Java encoding and JSON

Source: Internet
Author: User

1. Encoding

serialization (UrlEncode encoding): UrlEncode encoded

String a= "[{\" clubno\ ": \" 10000002\ ", \" clubtype\ ": \" 1\ "},{\" clubno\ ": \" 10000003\ ", \" clubtype\ ": \" 4\ "},{\" clubno\ ": \" 10000004\ ", \" clubtype\ ": \" 3\ "}]";

Encode string A through UrlEncode: a = Java.net.URLEncoder.encode (A, "utf-8");

deserialization (UrlDecode decoding):

String a= "%5b%7b%22clubno%22%3a%2210000002%22%2c%22clubtype%22%3a%221%22%7d%2c%7b%22clubno%22%3a%2210000003%22% 2C%22CLUBTYPE%22%3A%224%22%7D%2C%7B%22CLUBNO%22%3A%2210000004%22%2C%22CLUBTYPE%22%3A%223%22%7D%5D ";

A=java.net.urldecoder.decode (A, "UTF-8");

2.JSON

(1) Convert multiple strings to JSON data:

String a= "ERT";
String c= "KKKK";
Jsonobject json=new jsonobject ();
Json.put ("A", a);
Json.put ("C", c);
System.out.println (Json.tojsonstring ());

{"A": "ERT", "C": "KKKK"}

(2) parsing a JSON-type string:

String datas= "{\" imei\ ": \" 358732036143010\ ", \" version\ ": \" 2.3\ ", \" Apk\ ": [{\" appname\ ": \" synckey\ ", \" PackageName \ ": \" com.discovery.synckey\ "},{\" appname\ ": \" dbtest\ ", \" packagename\ ": \" com.discovery.synckey\ "},{\" appname\ " : \ "appupdate\", \ "packagename\": \ "Com.discovery.synckey\"}]} ";

Jsonobject Reqjson=jsonobject.parseobject (datas);

String imei=reqjson.getstring ("IMEI");

String apk=reqjson.getstring ("apk");//json type Collection

Jsonarray Parsearry =jsonobject.parsearray (APK);

Iterator<object> Iterator =parsearry.iterator ();

while (Iterator.hasnext ())
{
Jsonobject object = (jsonobject) iterator.next ();

String appname = (string) object.getstring ("appname")

}

(3).

Original data

Datas:  {"HMA17-KME-180425DNTN": "[Email protected]@3", "ym800_7.5_18031521": "[Email protected]@369"}
Parsed data
{"MaxPage": 2, "list": [{"Name": "HMA17-KME-180425DNTN", "num": "1531", "Actnum": "1531", "Weeknum": "3"},{"name": "ym800 _7.5_18031521 "," num ":" 745 "," actnum ":" 744 "," Weeknum ":" 369 "}]}

Jsonobject Reqjson=json.parseobject (datas);

For (map.entry<string, object> entry:reqJson.entrySet ()) {
System.out.println ("~!!!!!!!!!!!!!!!!!!!!!");
System.out.println (Entry.getkey ());
System.out.println (Entry.getvalue ());

String c= (String) entry.getvalue ();
String [] Res=c.split ("@");

String Name=entry.getkey ();

String Num=res[0];

String Actnum=res[1];

String weeknum=res[2];

}

(4)

String devices= "[email protected] @SNXYQLyR";

String [] Res=devices.split ("@");

List a=new Jsonarray ();

for (int i=1;i<res.length;i++)
{
A.add (Res[i]);
}

3. String comparisons are not case-sensitive

if (Res[i].tolowercase (). Contains (Deviceid.tolowercase ()))//Case insensitive comparison is equal

Club.replaceall ("\" \ "\" "," \ "") "; Remove the \ in the string club

A=a.replace ("\" "," ");//Remove the"

4. Access to the three-party interface

To add dependencies to a MAVEN project:

<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-kit</artifactId>
<version>1.3.4</version>
</dependency>

(1)

Url= "http:/" +ipnum+ "/wxserver/getonlinedeviceid?num=all";

HttpRequest req=httprequest.get (URL);

String Content=req.body (); What the three-party interface returns

(2) Post request

Openidurl Specific URLs

HttpRequest request = Httprequest.post (Openidurl). ContentType ("Application/json;charset=utf-8");
String res = Request.body (); What the three-party interface returns

Jsonobject obj = json.parseobject (res);

String access_token=obj.getstring ("Access_token"); A specific field value in a three-party interface

(3) Post request with data access three-party interface

Unurl Specific URLs

HttpRequest request = Httprequest.post (Unurl). ContentType ("Application/json;charset=utf-8"). Send (XML);
Returnxml=request.body ();

Java encoding and JSON

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.