Java code (processing JSON strings)

Source: Internet
Author: User

Package test;

Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONObject;

public class Dealjsonstring {
/* Parse the JSON text as Jsonobject or Jsonarray
public static final Object parse (String text)

Parse the JSON text into Jsonobject
public static final Jsonobject parseobject (String text)

Parse the JSON text into Jsonarray
public static final Jsonarray Parsearray (String text)

Convert JavaBean to Jsonobject or Jsonarray
public static Final Object ToJSON (object Javaobject)

Parse the JSON text to JavaBean
public static final T parseobject (String text, Class clazz)

Parse the JSON text into a JavaBean collection
public static final List Parsearray (String text, Class clazz)

Serializing JavaBean to JSON text
public static final String tojsonstring (Object object);

Serialize JavaBean to formatted JSON text
public static final String tojsonstring (Object object, Boolean Prettyformat)
*/

public static Vopersonid main (string[] args) {
String jsonStr1 = "{\" person\ ": {\" personid\ ": 100127,\" phone\ ": \" 13811821600\ "}}";

String jsonStr2 = "{\" code\ ": 0,\" data\ ": [{\" persontype\ ": 2,\" housepersonid\ ": 201010091001,\" type\ ": 3,\" personid\ " : 100684,},{\ "persontype\": 1,\ "housepersonid\": 201010091002,\ "type\": 2,\ "personid\": 201010091003,}],\ "DataExtra \ ": null,\" msg\ ": \" Ok\ "}";

String jsonStr3 = "{\" data\ ": {\" picturelist\ ": [{\" bigpicturefilepath\ ": \" http://ip:port//vpersonIdeoFile/ 201001021001.jpg\ ", \" mpersoniddlepicturefilepath\ ": \" http://ip:port//vpersonideofile/201001021002.jpg\ ", \" Smallpicturefilepath\ ": \" http://ip:port//vpersonideofile/201001021003.jpg\ "}],\" vpersonideofilepaht\ ": \" http:/ /ip:port//vpersonideofile/201001021001.mp4\ "},\" code\ ": 0,\" msg\ ": \" success \ "}";

String JSONSTR4 = "{\" listresult\ ": {\" curpage\ ": 1,\" resultcount\ ": 2,\" totalcount\ ": 32,\" pagesize\ ": 10,\" start\ ": 0 , \ "end\": 10,\ "ordername\": \ "personid\", \ "sortedby\": \ "desc\", \ "data\": Null,\ "page\": True},\ "data\": [{\ " Personid\ ": 201001021001,\" status\ ": 2,\" content\ ": \" Content 2\ ", \" category\ ": 3},{\" personid\ ": 201001021002,\" Status\ " : 2,\ "content\": Null,\ "category\": 3},{\ "personid\": 201001021003,\ "status\": 1,\ "content\": Null,\ "category\": 2},{ \ "personid\": 201001021004,\ "status\": 2,\ "content\": \ "Content 3\", \ "category\": 3}],\ "status\": 0,\ "msg\": null} ";

String JSONSTR5 = "{\" listresult\ ": {\" curpage\ ": 1},\" data\ ": {\" houses\ ": [{\" personid\ ": 201001021001,\" status\ ": 2, },{\ "personid\": 201001021101,\ "status\": 3},{\ "personid\": 201001021031,\ "status\": 3},{\ "personid\": 201001021303 , \ "status\": 3}],\ "overall\": {\ "paymoney\": 0,\ "housenum\": 2}},\ "status\": 0,\ "msg\": null} ";

String JSONSTR6 = "{\" listresult\ ": {\" curpage\ ": 1,\" resultcount\ ": 0,\" totalcount\ ": 0,\" pagesize\ ": 20,\" start\ ": 0, \ "end\": 20,\ "ordername\": \ "personid\", \ "sortedby\": \ "desc\", \ "data\": Null,\ "page\": True},\ "data\": {\ "houses\": [],\ "overall\": {\ "paymoney\": 0.0,\ "housenum\": 0}},\ "status\": 0,\ "msg\": null} ";

String jsonStr7 = "{\" code\ ": 0,\" msg\ ": \" 0\ ", \" data\ ": {\" paypaymoney\ ": null}}";

If the object is with Getjsonobject ("data"); If it is an array with Getjsonarray ("data") separate fields are obtained with getString ("");

Convert the corresponding result to a JSON object
Jsonobject jsonresponse = Json.parseobject (JSONSTR7);

Get the data JSON object
Jsonobject jsondata = jsonresponse.getjsonobject ("Data");

float paypaymoney=0.0f;
try {
String Temppaypaymoney = jsondata.getstring ("Paypaymoney");
Paypaymoney = Float.parsefloat (Temppaypaymoney);
} catch (Exception e) {
paypaymoney=0.0f;
}
System.out.println (string.valueof (Paypaymoney));
}

}

Java code (processing JSON strings)

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.