1.获取Post传送的数据
这里我们借助Apache Commons IO中的IOUtils类
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
将Post的数据流转成String
如果不用commons io类库,自己转换的方法
public static String receivePost(HttpServletRequest request) throws IOException, UnsupportedEncodingException {
// 读取请求内容
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
// 将资料解码
String reqBody = sb.toString();
return URLDecoder.decode(reqBody, HTTP.UTF_8);
}
或者
InputStream sin = new BufferedInputStream(request.getInputStream()); ByteArrayOutputStream sout = new ByteArrayOutputStream(); int b=0; while((b=sin.read())!=-1) { sout.write(b); } byte[] temp = sout.toByteArray(); String s_ok = new String(temp,"UTF-8");
2.
使用org.json和Newtonsoft.Json
Json 对象的生成和操作
newtonsoft.json
JObject j = new JObject(); j["id"] = 32; j.Add("name", "John"); Console.WriteLine(j.ToString()); Console.WriteLine("ID和姓名分别是:" + (int)j["id"] + " " + j["name"]);
org.json
JSONObject j=new JSONObject(); j.put("id", 32); j.put("name", "John"); System.out.println(j.toString()); System.out.println("ID和姓名分别是:"+j.getInt("id")+" "+j.getString("name"));
注意newtonsoft添加和获取键值对使用 ["key"],上面都输出
{"name":"John","id":32}
ID和姓名分别是:32 John
由字符串生成json对象
newtonsoft.json
string str = "{"id":7,"status":"good","gender":"male","addr":"mit"}"; //下面两行等效 JObject J = JObject.Parse(str); JObject j = JsonConvert.DeserializeObject(str) as JObject; j["status"] = "baaaaaad"; Console.WriteLine(j.ToString()); String str2 = "[{ID:'1',Name:'John'},{ID:'2',Name:'Alan'}]"; JArray ar = JsonConvert.DeserializeObject(str2) as JArray;
//遍历Array对象 for (int i = 0; i < ar.Count; i++) { JObject tmp = ar[i] as JObject; Console.WriteLine(tmp); }
org.json
String str="{"id":7,"status":"good","gender":"male","addr":"mit"}"; JSONObject j=new JSONObject(str); j.put("status", "baaaaaad"); System.out.println(j.toString()); String str2="[{ID:'1',Name:'John'},{ID:'2',Name:'Alan'}]"; JSONArray ar=new JSONArray(str2); //遍历Array对象 for(int i=0;i<ar.length();i++) { JSONObject tmp=ar.getJSONObject(i); System.out.println(tmp); }
上面 都输出
{"gender":"male","id":7,"addr":"mit","status":"baaaaaad"}
{"ID":"1","Name":"John"}
{"ID":"2","Name":"Alan"}
由普通对象和数据结构生成json对象
关于Person类
//在C#中 class Person { public int id { set; get; } public string name { set; get; } public string place { set; get; } } //在Java中 public class Person { Integer id; String name; String place; public Person() { } .... //getter and setter
newtonsoft.json
Person p = new Person(); p.id = 32; p.name = "Jack"; p.place = "New York"; JObject j = JObject.FromObject(p); Console.WriteLine(j); Dictionary<string, object> d = new Dictionary<string, object>(); d.Add("name", "Alan"); d.Add("age", 44); d.Add("title", "Worker"); JObject j1 = JObject.FromObject(d); Console.WriteLine(j1);
org.json
Person p=new Person(); p.setId(32); p.setName("Jack"); p.setPlace("New York"); JSONObject j=new JSONObject(p); System.out.println(j.toString()); Map<String,Object> m=new HashMap<String,Object>(); m.put("name", "Alan"); m.put("age", 44); m.put("title", "worker"); JSONObject j1=new JSONObject(m); System.out.println(j1.toString());
以上都输出
{"name":"Jack","id":32,"place":"New York"}
{"name":"Alan","title":"worker","age":44}
newtonsoft的json字符串和普通对象的直接转换
对象到字符串
String str=JsonConvert.SerializeObject(p);
字符串到对象
Person p1= JsonConvert.DeserializeObject<Person>(str);
或者
Person p2 = JsonConvert.DeserializeObject(str, typeof(Person)) as Person;
3相关库的获取
获取Newtonsoft.Json包
到https://www.nuget.org/downloads 下载VS2013的插件,安装好之后。
打开你的工程,然后选择菜单中的工具
点击控制台,输入
PM> Install-Package Newtonsoft.Json
这样Newtonsoft.Json 就被添加到引用中了。
获取org.json
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>