maven项目对json字符串进行解析
2017-09-27 09:49
323 查看
<!-- org.json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
案例:
package com.yanshu.test;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtil {
public static void main(String[] args) throws JSONException {
String jsonContent = "{'hello':world,'abc':'xyz'}";
JSONObject jsonObject = new JSONObject(jsonContent);
String str1 = jsonObject.getString("hello");
String str2 = jsonObject.getString("abc");
System.out.println(str1);
System.out.println(str2);
}
}
案例
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String args[]) throws JSONException {
String jsonContent = "{'hello':world,'abc':'xyz'}";
JSONObject jsonObject = new JSONObject(jsonContent);
String str1 = jsonObject.getString("hello");
String str2 = jsonObject.getString("abc");
System.out.println(str1);
System.out.println(str2);
System.out.println("------------------");
jsonContent = "[{'hello':333,'abc':'false','xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':'true','xyz':{'a':2,'b':'ba'}}]";
JSONArray jsonArray = new JSONArray(jsonContent);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject2=jsonArray.getJSONObject(i);
int value1=jsonobject2.getInt("hello");
boolean value2=jsonobject2.getBoolean("abc");
// String value3=jsonobject2.getString("xyz");
JSONObject jsonobject3=jsonobject2.getJSONObject("xyz");
int value4=jsonobject3.getInt("a");
String value5=jsonobject3.getString("b");
System.out.println(value1);
System.out.println(value2);
System.out.println(value4);
System.out.println(value5);
}
}
}
输出结果
world
xyz
------------------
333
false
1
ab
555
true
2
ba
我的文件资源有json的jar包
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
案例:
package com.yanshu.test;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtil {
public static void main(String[] args) throws JSONException {
String jsonContent = "{'hello':world,'abc':'xyz'}";
JSONObject jsonObject = new JSONObject(jsonContent);
String str1 = jsonObject.getString("hello");
String str2 = jsonObject.getString("abc");
System.out.println(str1);
System.out.println(str2);
}
}
案例
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String args[]) throws JSONException {
String jsonContent = "{'hello':world,'abc':'xyz'}";
JSONObject jsonObject = new JSONObject(jsonContent);
String str1 = jsonObject.getString("hello");
String str2 = jsonObject.getString("abc");
System.out.println(str1);
System.out.println(str2);
System.out.println("------------------");
jsonContent = "[{'hello':333,'abc':'false','xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':'true','xyz':{'a':2,'b':'ba'}}]";
JSONArray jsonArray = new JSONArray(jsonContent);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject2=jsonArray.getJSONObject(i);
int value1=jsonobject2.getInt("hello");
boolean value2=jsonobject2.getBoolean("abc");
// String value3=jsonobject2.getString("xyz");
JSONObject jsonobject3=jsonobject2.getJSONObject("xyz");
int value4=jsonobject3.getInt("a");
String value5=jsonobject3.getString("b");
System.out.println(value1);
System.out.println(value2);
System.out.println(value4);
System.out.println(value5);
}
}
}
输出结果
world
xyz
------------------
333
false
1
ab
555
true
2
ba
我的文件资源有json的jar包
相关文章推荐
- java解析json格式的字符串 maven依赖
- logstash中字符串的split,对每个子串进行json解析
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
- java直接访问链接url,并对返回的json字符串进行解析
- 对JSON字符串进行解析
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
- java解析json格式的字符串 maven依赖
- 解析json字符串并进行图片位置的替换
- 使用com.google.gson包进行json字符串的构造与解析
- C#项目中Json字符串解析
- 从零开发一个完整的Android项目(五)——解析JSON字符串
- eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返回解析。 使用eval
- json_encode不对中文进行转码、json_decode解析json字符串返回NULL的处理方法
- Ajax使用一+javascript解析Ajax返回的json字符串
- JavaScript解析json字符串
- 浏览器[IE] Ajax Struts Json返回json字符串,解析不了 提示保存或下载 解决方式
- MVC Newtonsoft.Json解析JSON字符串
- C#解析JSON字符串总结
- 基于开源库jsoncpp的json字符串解析
- java如何解析json字符串