Because the company in recent days to get the project, and the official API provided by the interface, some need to post the request, and data transmission format requirements for JSON format data, did not study before, and the data format is relatively simple to pass, So the direct is to use the string splicing way to assemble, and then use String.Format format a bit better.
JSON data to be submitted string jsondata = "{\" openid\ ": \"%s\ ", \" to_groupid\ ":%d}";//Call Interface Mobile User Group Jsonobject Jsonobject = Weixinutil.httprequest (Requesturl, "POST", String.Format (Jsondata, openid,groupid));
However, the use of the message template interface today, found that the POST request data a bit more, if still in this way it seems a little trouble, and is not very intuitive, need to do the double quotation marks to do the escape operation, sometimes turn more look at the dizziness. So the specific check of the information of their own study a bit.
First, because JSON is used, the JSON development kit is first imported into the project for conversion between Java objects and JSON strings;
A total of 3 Jar:ezmorph-1.0.6.jar,json-lib-2.2.3-jdk13.jar and Morph-1.1.1.jar are in the JSON development kit.
These three jar kits have been uploaded to Csdn. You can download it if you need it:
http://download.csdn.net/detail/hu1991die/8401677
Example code:
Package Com.json;import Java.util.iterator;import Net.sf.json.jsonarray;import net.sf.json.jsonobject;/** * Java operation JSON Format data * @author Feizi * @time 2015-1-27 pm 5:36:13 */public class Testjson {public static void main (string[] args) {//jsonobject:{' right ': ' Right ', ' left ': ' Left ', ' obj ': {' second ': ' 222 ', ' third ': ' 333 '}, ' Ele ': {' four ': ' 444 ', ' Five ': ' 555 "}," Data ": [{" Name ":" Zhang San "," Age ":" Max "," Sex ":" Male "},{" name ":" Lie Triple "," Age ":" A "," sex ":" Female "}]} jsonobject jsonobject = Testjson.createjsonobject (); Output Jsonobject Object System.out.println ("Jsonobject:" + jsonobject); Returns a string by key, string right = Jsonobject.getstring ("right"); System.out.println ("right==>" + right); Right==>right Jsonobject obj = (jsonobject) jsonobject.get ("obj"); System.out.println ("obj==>" +obj);//obj==>{"second": "222", "third": "333"} String second = (obj = = null?) "": (String) Obj.get ("second")); System.out.println ("second==>" + second); second==>222 Jsonarray ARRay = Jsonobject.getjsonarray ("Data"); System.out.println ("array==>" +array);//array==>[{"name": "Zhang San", "Age": "All", "sex": "Male"},{"name": "Lie Triple", "Age": " "," "Sex": "female"}] if (!array.isempty ()) {for (int i = 0; i < array.size (); i++) {//{"name": "Zhang San", "Age": "Max", "se X ":" Male "}//{" name ":" Lie Triple "," Age ":" A "," sex ":" female "} jsonobject element = (jsonobject) array.get (i);//array.get (i) method returns OB by default Ject type, need to shape into Jsonobject type to remove the key value System.out.println ("element==>" +element); String name = (string) element.get ("name"); String age = (string) element.get ("Age"); String sex = (string) element.get ("Sex"); Name==> Zhang San//age==>23//sex==> male//name==> lie triple//age==>20//sex==> female System.out.println (" Name==> "+name); System.out.println ("age==>" +age); System.out.println ("sex==>" +sex); }//or iterate for with Iterator iterator for (iterator<object> Iterator = Array.iterator (); Iterator.hasnext ();) {jsonobject element = (jsonobject) IteraTor.next (); System.out.println ("element==>" +element); And then the same as above}}}public static Jsonobject Createjsonobject () {Jsonobject jsonobject = new Jsonobject ();//General format//jsono bject:{' right ': ' Right ', ' left ': ' Left '}jsonobject.put (' right ', ' right '), Jsonobject.put (' left ', ' left ');//nested Format// jsonobject:{"obj": {"second": "222", "third": "333"}, "Ele": {"four": "444", "Five": "555"}}jsonobject jsonobj = new Jsonobject (); Jsonobj.put ("Second", "222"), Jsonobj.put ("third", "333"); Jsonobject ele = new Jsonobject (), Ele.put ("Four", "444"), Ele.put ("Five", "555"), Jsonobject.element ("obj", jsonobj); Jsonobject.element ("Ele", ele);//array nested format//jsonobject:{"data": [{"Name": "Zhang San", "Age": "+", "Sex": "Male"},{"name": "Lie Triple", " Age ":", "Sex": "female"}]}jsonobject dataEle1 = new Jsonobject ();d ataele1.put ("name", "Zhang San");d ataele1.put ("Age", "23"); Dataele1.put ("Sex", "male"); Jsonobject dataele2=new jsonobject (); Dataele2.put ("name", "Lie Triple"); Dataele2.put ("Age", "20"); Dataele2.put ("Sex", "female"); Jsonarray Jsonarray = new Jsonarray (); jSonarray.add (0, DataEle1); Jsonarray.add (1, dataEle2); Jsonobject.accumulate ("Data", jsonarray);// Jsonobject.element ("Data", jsonarray);//It seems that both methods can be, I did not go into detail, so I do not know what the difference between them return jsonobject;}
About the template message interface in the API:
Post Data description
Examples of post data are:
{"Tous ER ":" OPENID "," template_id ":" Ngqipbwh8bufcssecmogfxcv14j0tqlepbo27izeyty "," url ":" Http://weixin.qq.com /download "," Topcolor ":" #FF0000 "," data ": {" First ": {" value ": "Congratulations on your successful purchase!" "," Color ":" #173177 "}," Keynote1 ": {" Valu E ":" Chocolate "," Color ":" #173177 "}," Keynote2 ": { "Value": "39.8 Yuan", "Color": "#173177"}, "Keynote3": { "Value": "September 16, 2014", "Color": "#173177"}, "remark" : {"value": "Welcome to purchase again!" "," Color ":" #173177 "}}}
Packaged in Java and assembled into the JSON format data above
My method is rather stupid, only for reference:
public class Jsontemplate {private string touser;//Receiver private string template_id;//template Idprivate string url;// URL address private string topcolor;//top color Private jsonobject data;//content public jsontemplate () {}public string gettouser () { return touser;} public void Settouser (String touser) {this.touser = Touser;} Public String gettemplate_id () {return template_id;} public void settemplate_id (String template_id) {this.template_id = template_id;} Public String GetUrl () {return URL;} public void SetUrl (String url) {this.url = URL;} Public String Gettopcolor () {return topcolor;} public void Settopcolor (String topcolor) {this.topcolor = Topcolor;} Public Jsonobject GetData () {return data;} public void SetData (Jsonobject data) {this.data = data;}}
Package com.wx.pojo.template;/** * Template message content * @author Feizi * @time 2015-1-27 pm 2:53:49 */public class Dataelement {private String title;//header name private string value;//content private string color;//color public dataelement () {}public dataelement (String ti Tle, string value, String color) {this.title = Title;this.value = Value;this.color = color;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public String GetValue () {return value;} public void SetValue (String value) {this.value = value;} Public String GetColor () {return color;} public void SetColor (String color) {this.color = color;}}
Test the JSON example above:
/** * Assembly JSON format * @param tmp * @param dataList * @return */public static jsonobject createjsonobject (Jsontemplate tmplate,l Ist<dataelement> dataList) {jsonobject Datajson = new Jsonobject (); if (dataList! = null && datalist.size () & Gt 0) {Jsonobject Elementjson = null;for (dataelement data:datalist) {Elementjson = new jsonobject (); Elementjson.put ("Value ", Data.getvalue ()); Elementjson.put (" Color ", Data.getcolor ());d Atajson.put (Data.gettitle (), Elementjson);}} Jsonobject Tmpjson = new Jsonobject () tmpjson.put ("Touser", Tmplate.gettouser ()); Tmpjson.put ("template_id", TMPLATE.GETTEMPLATE_ID ()); Tmpjson.put ("url", Tmplate.geturl ()); Tmpjson.put ("Topcolor", Tmplate.gettopcolor ()); Tmpjson.put ("Data", Datajson); return Tmpjson;} public static void Main (string[] args) {jsontemplate tmp = new jsontemplate (); Tmp.settouser ("OPENID"); Tmp.settemplate_ ID ("Ngqipbwh8bufcssecmogfxcv14j0tqlepbo27izeyty"); Tmp.seturl ("Http://weixin.qq.com/download"); Tmp.settopcolor ( "#FFF");D ataelement data0 = new DaTaelement ();//Because the parameter constructor has been rewritten, it is also possible to invoke the parameter constructor directly for initialization data0.settitle ("first");d Ata0.setvalue ("Congratulations on your purchase success!) ");d Ata0.setcolor (" #173177 ");D ataelement data1 = new Dataelement ();d ata1.settitle (" keynote1 ");d ata1.setvalue (" Chocolate " );d Ata1.setcolor ("#173177");D ataelement data2 = new Dataelement ();d ata2.settitle ("Keynote2");d ata2.setvalue ("39.8 yuan ");d Ata2.setcolor (" #173177 ");D ataelement data3 = new Dataelement ();d ata3.settitle (" Keynote3 ");d Ata3.setvalue (" September 16, 2014 ");d ata3.setcolor (" #173177 ");D ataelement data4 = new Dataelement ();d ata4.settitle (" remark "); Data4.setvalue ("Welcome to buy again!") ");d Ata4.setcolor (" #173177 "); list<dataelement> dataList = new arraylist<dataelement> ();d atalist.add (data0);d atalist.add (data1); Datalist.add (data2);d atalist.add (data3);d atalist.add (DATA4); Jsonobject Tmpjson = Jsontemplate.createjsonobject (tmp, dataList); System.out.println (Tmpjson);}
After the run, the console output results:
{"Touser": "OPENID", "template_id": "Ngqipbwh8bufcssecmogfxcv14j0tqlepbo27izeyty", "url": "http://weixin.qq.com/ Download "," Topcolor ":" #FFF "," data ": {" First ": {" value ":" Congratulations on your successful purchase! " "," Color ":" #173177 "}," Keynote1 ": {" value ":" Chocolate "," Color ":" #173177 "}," Keynote2 ": {" value ":" $39.8 "," Color ":" #173177 "}," Keynote3 ": {" value ":" September 16, 2014 "," Color ":" #173177 "}," remark ": {" value ":" Welcome to purchase again! " "," Color ":" #173177 "}}}
Java Operation JSON Format data