繁体   English   中英

如何在Java中解析以下json? 我尝试使用简单的json,但出现了空指针异常

[英]How to parse following json in Java? I tried using simple json but I am getting a null pointer exception

我的Json是以下类型:

    {
  "result": [
    {
      "skills": "alexa",
      "brand": "amazon",
      "name": "jack",
      "type": "noob",
      "id": "1230",
      "date": "8-5-2016"
    }
  ]
}

我想从这个json.name和id获取名称。我尝试使用简单的json和gson nut尝试2级,即访问“名称”变量时,我得到一个空引用指针错误。

另请注意,我是从URL提取的。 谢谢 !!


我的Java代码是

    JSONObject obj1= new JSONObject(line);
    JSONArray cobj = (JSONArray)obj1.getJSONArray("result");
    JSONObject ccobj = (JSONObject)cobj.get(1);
    System.err.println(ccobj.getString("result"));

您的json无效。

Error: Parse error on line 6:
...mazon"       },      "name": "jack",     "type": 
---------------------^
Expecting 'EOF', '}', ',', ']', got ':'

一旦您纠正了,我建议您使用Gson。

创建这些类

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("result")
@Expose
private List<Result> result = new ArrayList<Result>();

/**
* 
* @return
* The result
*/
public List<Result> getResult() {
return result;
}

/**
* 
* @param result
* The result
*/
public void setResult(List<Result> result) {
this.result = result;
}

}
-----------------------------------com.example.Result.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("skills")
@Expose
private String skills;
@SerializedName("brand")
@Expose
private String brand;
@SerializedName("name")
@Expose
private String name;
@SerializedName("type")
@Expose
private String type;
@SerializedName("id")
@Expose
private String id;
@SerializedName("date")
@Expose
private String date;

/**
* 
* @return
* The skills
*/
public String getSkills() {
return skills;
}

/**
* 
* @param skills
* The skills
*/
public void setSkills(String skills) {
this.skills = skills;
}

/**
* 
* @return
* The brand
*/
public String getBrand() {
return brand;
}

/**
* 
* @param brand
* The brand
*/
public void setBrand(String brand) {
this.brand = brand;
}

/**
* 
* @return
* The name
*/
public String getName() {
return name;
}

/**
* 
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}

/**
* 
* @return
* The type
*/
public String getType() {
return type;
}

/**
* 
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}

/**
* 
* @return
* The id
*/
public String getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

/**
* 
* @return
* The date
*/
public String getDate() {
return date;
}

/**
* 
* @param date
* The date
*/
public void setDate(String date) {
this.date = date;
}

}

那么你会做这样的事情

Gson gson = new Gson();
Example ex = gson.fromJson(jsonString,Example.class);

然后从示例对象获取结果,进行迭代,提取出所需的任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM