JSON

path学习笔记二(2)

字号+ 作者:H5之家 来源:H5之家 2015-10-21 17:30 我要评论( )

JSONValue.parseStrict()和JSONValue.parseWithException() 都会抛出ParseException异常,单从输出结果看二者是一样的,区别是JSONValue.parseStrict()的解析输入的json文本的有效性是依据RFC4627。而JSONValue.par

JSONValue.parseStrict()和JSONValue.parseWithException() 都会抛出ParseException异常,单从输出结果看二者是一样的,区别是JSONValue.parseStrict()的解析输入的json文本的有效性是依据RFC4627。而JSONValue.parseWithException()解析输入的json文本内容依据默认的解析文本的模式。

JsonValue的remapField方法以及对于编码转换 import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringReader; import net.minidev.json.JSONObject; import net.minidev.json.JSONValue; import net.minidev.json.writer.JsonReader; public class JsonReaderTest { static String[] nonLatinTexts = new String[] { "????? ?????", "日本?", "Русский", "?????", "???", "???????", "??????", "?????", "中文", "????", "??????", "??????", "?????????" }; public static void main(String[] args) throws IOException { String text = "{'new':'foo','default':'bar'}"; JSONValue.remapField(TRen.class, "default", "default_"); JSONValue.remapField(TRen.class, "new", "new_"); TRen t = JSONValue.parse(text, TRen.class); System.out.println(JSONValue.toJSONString(t)); System.out.println("*******************************"); for (String nonLatinText : nonLatinTexts) { String s = "{\"key\":\"" + nonLatinText + "\"}"; // 1. StringReader reader = new StringReader(s); // JSONObject obj = (JSONObject) JSONValue.parse(reader); // 2. ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes("utf8")); // JSONObject obj = (JSONObject) JSONValue.parse(bis); // 3. byte[] bs = s.getBytes("utf8"); // JSONObject obj = (JSONObject) JSONValue.parse(bs); JSONObject obj = (JSONObject) JSONValue.parse(s); String v = (String) obj.get("key"); // result is incorrect System.out.print(v + ", "); } System.out.println(); } public static class TRen { public String new_; public String default_; } }

输出

{"default":"bar","new":"foo"} ******************************* ????? ?????, 日本?, Русский, ?????, ???, ???????, ??????, ?????, 中文, ????, ??????, ??????, ?????????, Json转Object示例 import java.util.ArrayList; import java.util.List; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; import net.minidev.json.JSONValue; public class JsonToObject { public static void main(String[] args) { People people = new People(); people.setName("test1"); people.setAge(13); people.setMobile(20130808); People people1 = new People(); people1.setName("test"); people1.setAge(123); people1.setMobile(888666); List<People> array = new ArrayList<People>(); array.add(people); array.add(people1); JSONObject obj = new JSONObject(); obj.put("peoples", array); System.out.println(JSONValue.toJSONString(obj)); JSONArray jsonArray = new JSONArray(); jsonArray.add(people); jsonArray.add(people1); JSONObject result = new JSONObject(); result.put("peoples", jsonArray); System.out.println(JSONValue.toJSONString(result)); } } class People { public String name; public int age; public boolean single; public long mobile; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSingle() { return single; } public void setSingle(boolean single) { this.single = single; } public long getMobile() { return mobile; } public void setMobile(long mobile) { this.mobile = mobile; } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评