在Java中解析JSON对象
我有如下JSON对象:在Java中解析JSON对象,java,json,Java,Json,我有如下JSON对象: member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}"; 在Java中,我想解析上面的json对象并将值存储在arraylist中 我正在寻找一些代码来实现这一点。我假设您希望将感兴趣的键存储在列表中 使用图书馆: JSONObject obj=newjsonobject({interests:[{interestKey:Dogs},{interestKey:Cats}]); 列表=新的Arra
member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";
在Java中,我想解析上面的json对象并将值存储在arraylist中
我正在寻找一些代码来实现这一点。我假设您希望将感兴趣的键存储在列表中 使用图书馆:
JSONObject obj=newjsonobject({interests:[{interestKey:Dogs},{interestKey:Cats}]);
列表=新的ArrayList();
JSONArray数组=obj.getJSONArray(“兴趣”);
对于(int i=0;i
公共类JsonParsing{
公共静态属性=null;
公共静态JSONObject JSONObject=null;
静止的{
属性=新属性();
}
公共静态void main(字符串[]args){
试一试{
JSONParser JSONParser=新的JSONParser();
File File=new文件(“src/main/java/read.json”);
objectobject=jsonParser.parse(新文件读取器(文件));
jsonObject=(jsonObject)对象;
parseJson(jsonObject);
}捕获(例外情况除外){
例如printStackTrace();
}
}
公共静态void getArray(对象object2)引发ParseException{
JSONArray jsonArr=(JSONArray)object2;
for(int k=0;k
非常感谢@code的另一个回答。由于您的代码,我可以读取任何JSON文件。现在,我正在尝试按级别组织所有元素,因为我可以使用它们
我正在使用Android从URL读取JSON,唯一需要更改的是行
Set Set=jsonObject.keySet();
迭代器迭代器=set.Iterator()代码>
为了
Iterator Iterator=jsonObject.keys()代码>
我分享我的实现,以帮助他人:
public void parseJson(JSONObject jsonObject) throws ParseException, JSONException {
Iterator<?> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String obj = iterator.next().toString();
if (jsonObject.get(obj) instanceof JSONArray) {
//Toast.makeText(MainActivity.this, "Objeto: JSONArray", Toast.LENGTH_SHORT).show();
//System.out.println(obj.toString());
TextView txtView = new TextView(this);
txtView.setText(obj.toString());
layoutIzq.addView(txtView);
getArray(jsonObject.get(obj));
} else {
if (jsonObject.get(obj) instanceof JSONObject) {
//Toast.makeText(MainActivity.this, "Objeto: JSONObject", Toast.LENGTH_SHORT).show();
parseJson((JSONObject) jsonObject.get(obj));
} else {
//Toast.makeText(MainActivity.this, "Objeto: Value", Toast.LENGTH_SHORT).show();
//System.out.println(obj.toString() + "\t"+ jsonObject.get(obj));
TextView txtView = new TextView(this);
txtView.setText(obj.toString() + "\t"+ jsonObject.get(obj));
layoutIzq.addView(txtView);
}
}
}
}
public void parseJson(JSONObject JSONObject)抛出ParseException,jsoneexception{
迭代器迭代器=jsonObject.keys();
while(iterator.hasNext()){
String obj=iterator.next().toString();
if(jsonObject.get(obj)JSONArray实例){
//Toast.makeText(MainActivity.this,“Objeto:JSONArray”,Toast.LENGTH_SHORT.show();
//System.out.println(obj.toString());
TextView txtView=新的TextView(此);
setText(obj.toString());
layoutIzq.addView(txtView);
getArray(jsonObject.get(obj));
}否则{
if(jsonObject.get(obj)jsonObject实例){
//Toast.makeText(MainActivity.this,“Objeto:JSONObject”,Toast.LENGTH\u SHORT.show();
parseJson((JSONObject)JSONObject.get(obj));
}否则{
//Toast.makeText(MainActivity.this,“Objeto:Value”,Toast.LENGTH_SHORT.show();
//System.out.println(obj.toString()+“\t”+jsonObject.get(obj));
TextView txtView=新的TextView(此);
setText(obj.toString()+“\t”+jsonObject.get(obj));
layoutIzq.addView(txtView);
}
}
}
}
Java中有许多可用的JSON库
最臭名昭著的是:Jackson、GSON、Genson、FastJson和org.json
在选择任何库时,通常应考虑三件事:
演出
易用性(代码编写简单易读)-这与功能相匹配
对于移动应用程序:依赖项/jar大小
特别是对于JSON库(以及任何序列化/反序列化库),数据绑定通常也很有趣,因为它不需要编写锅炉板代码来打包/解包数据
对于1,请参阅此基准测试:我使用它比较了(jackson、gson、genson、fastjson、org.json、jsonp)使用流API和数据绑定API的序列化器和反序列化器的性能。
对于2,你可以在网上找到很多例子。上面的基准测试也可以用作示例源
快速获取基准:Jackson的性能比org.json好5到6倍,比GSON好两倍多
对于您的特定示例,以下代码使用jackson解码json:
public class MyObj {
private List<Interest> interests;
static final class Interest {
private String interestKey;
}
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) throws IOException {
MyObj o = JACKSON.readValue("{\"interests\": [{\"interestKey\": \"Dogs\"}, {\"interestKey\": \"Cats\" }]}", MyObj.class);
}
}
公共类MyObj{
私人上市权益;
静态最终类兴趣{
私钥;
}
私有静态最终ObjectMapper MAPPER=新ObjectMapper();
公共静态void main(字符串[]args)引发IOException{
MyObj o=JACKSON.readValue(“{\'interestKey\':[{\'interestKey\':\'Dogs\',{\'interestKey\':\'Cats\'}]),MyObj.class);
}
}
如果您有任何问题,请告诉我。1。)创建适当类型的arraylist,在本例中,即String
2.)创建一个JSONObject
,同时将字符串作为输入传递给JSONObject
构造函数
- As
JSONObject
符号用大括号表示,即{}
- 其中as
JSONArray
符号用方括号表示,即[]
3.)从JSONObject
中检索JSONArray
(在第2页创建)
public void parseJson(JSONObject jsonObject) throws ParseException, JSONException {
Iterator<?> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String obj = iterator.next().toString();
if (jsonObject.get(obj) instanceof JSONArray) {
//Toast.makeText(MainActivity.this, "Objeto: JSONArray", Toast.LENGTH_SHORT).show();
//System.out.println(obj.toString());
TextView txtView = new TextView(this);
txtView.setText(obj.toString());
layoutIzq.addView(txtView);
getArray(jsonObject.get(obj));
} else {
if (jsonObject.get(obj) instanceof JSONObject) {
//Toast.makeText(MainActivity.this, "Objeto: JSONObject", Toast.LENGTH_SHORT).show();
parseJson((JSONObject) jsonObject.get(obj));
} else {
//Toast.makeText(MainActivity.this, "Objeto: Value", Toast.LENGTH_SHORT).show();
//System.out.println(obj.toString() + "\t"+ jsonObject.get(obj));
TextView txtView = new TextView(this);
txtView.setText(obj.toString() + "\t"+ jsonObject.get(obj));
layoutIzq.addView(txtView);
}
}
}
}
public class MyObj {
private List<Interest> interests;
static final class Interest {
private String interestKey;
}
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) throws IOException {
MyObj o = JACKSON.readValue("{\"interests\": [{\"interestKey\": \"Dogs\"}, {\"interestKey\": \"Cats\" }]}", MyObj.class);
}
}
`"{"key":"value"}"`
`"{\"key\":\"value\"}"`
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(inputString);
JSONParser parser = new JSONParser();
String response = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";
JSONObject jsonObj = (JSONObject) parser.parse(response);
JSONObject jsonObj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");
List<String> interestList = new ArrayList<String>();
JSONArray jsonArray = jsonObj.getJSONArray("interests");
for(int i = 0 ; i < jsonArray.length() ; i++){
interestList.add(jsonArray.getJSONObject(i).optString("interestKey"));
}
public String optString(String key, String defaultValue) {
String missingKeyValue = json_data.optString("status","N/A");
// note there is no such key as "status" in response
// will return "N/A" if no key found
String missingKeyValue = json_data.optString("status");
// will return "" if no key found where "" is an empty string