java json字符串处理
2013-07-12 17:07
295 查看
使用Java操作JSON字符串对象
http://www.blogjava.net/Werther/archive/2010/01/20/310262.html
代码样例:
// 输出结果为{"version": 4,"addr": "192.160.1.11"}
private static void test1()
{
String s = "{\"internal_1\": [{\"version\": 4,\"addr\": \"192.160.1.11\"}]}";
String regex = ".+?\\[(.+?)\\].+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
// 输出结果为{"Done":1,"ReturnType":1,"Msg":"HELLO,上海"}
private static void test2()
{
String s="TestJsonResponse{TestJsonResult={\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"};}";
String regex = ".+?\\=(.+?)\\;.+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
//结果为HELLO,上海
private static void test3()
{
/*JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("a", 1);
jsonObject.put("b", 1.1);
jsonObject.put("c", 1L);
jsonObject.put("d", "test");
jsonObject.put("e", true);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jsonObject); // 输出{"d":"test","e":true,"b":1.1,"c":1,"a":1}
try {
System.out.println(jsonObject.getString("d"));// 输出test
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
String s="{\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"}";
try {
JSONObject jsonObject2 = new JSONObject(s);
System.out.println(jsonObject2.getString("Msg")); // 输出HELLO,上海
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
http://www.blogjava.net/Werther/archive/2010/01/20/310262.html
Java解析Json(org.json,json-lib)
http://ysj5125094.iteye.com/blog/1633238代码样例:
// 输出结果为{"version": 4,"addr": "192.160.1.11"}
private static void test1()
{
String s = "{\"internal_1\": [{\"version\": 4,\"addr\": \"192.160.1.11\"}]}";
String regex = ".+?\\[(.+?)\\].+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
// 输出结果为{"Done":1,"ReturnType":1,"Msg":"HELLO,上海"}
private static void test2()
{
String s="TestJsonResponse{TestJsonResult={\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"};}";
String regex = ".+?\\=(.+?)\\;.+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
//结果为HELLO,上海
private static void test3()
{
/*JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("a", 1);
jsonObject.put("b", 1.1);
jsonObject.put("c", 1L);
jsonObject.put("d", "test");
jsonObject.put("e", true);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jsonObject); // 输出{"d":"test","e":true,"b":1.1,"c":1,"a":1}
try {
System.out.println(jsonObject.getString("d"));// 输出test
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
String s="{\"Done\":1,\"ReturnType\":1,\"Msg\":\"HELLO,上海\"}";
try {
JSONObject jsonObject2 = new JSONObject(s);
System.out.println(jsonObject2.getString("Msg")); // 输出HELLO,上海
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- 关于PHP处理JAVA过来的接口和PHP返回给Javascript的JSON字符串
- Java利用gson处理json字符串
- java处理Json大字符串和常用数据结构的相互转换
- 使用json.org和json-lib来处理json字符串和java对象的转化
- java处理json字符串实例
- java 处理json字符串中null值
- java 处理 json字符串
- js中java中对json字符串、json对象、jsonArray的处理
- java对json数组格式的字符串的处理
- java转json 报错处理机制
- json字符串转java对象数组
- java判断一个字符串是否是json格式
- Java字符串处理String、StringBuilder、StringBuffer类效率分析
- JSON.stringify()将JSON对象转换为字符串通过Ajax传入到后台处理
- js读Json,JSON与字符串转化,Java读JSON
- java 字符串常用处理
- Java中字符串中的空格的处理
- java对象转json字符串工具Gson简单入门
- C#处理Json字符串、Json对象、多层嵌套
- 将json字符串转化为json数组,以及转化为java中的集合