使用Gson/Jackson Java API解析JSON文件

4
我是一个对JSON和GSON都很新的人。我的JSON结构如下所示,我正在使用gson库解析json对象并检索值,但是从getter中获取到了所有的空值。有人能帮帮我吗。
JSON文件:
{
   "Samples":[
      {
       "Id":"XX",
       "SampleId":"XX",
       "Gender":"XX"
       }
     ]
}

Java-gson 代码:

BufferedReader br = new BufferedReader(new FileReader(/mnt/ftp/sample.json));
//convert the json string back to object
patientObj = gson.fromJson(br, PatientJson.class);
patient_id=patientObj.getId();
sample_id=patientObj.getSampleId();
gender=patientObj.getGender();

患者JSON类:
public class PatientJson {
    String id,sampleId,gender;
//with all the three getter and setters.
}

展示你的 PatientJson 类。 - Manish
@user3746601 我不太确定,但请检查字符的大小写。例如,如果字符串是 id,请确保 JSON 也是 id 而不是 Id - Sarp Kaya
有人能帮我解决这个问题吗?可以使用gson或jackson API吗? - Abhi.G
2个回答

1
我希望它能帮助你..!
    JSONObject jsonObject=new JSONObject(readFromFile());  
    JSONArray array= jsonObject.getJSONArray("Samples");  

        JSONObject json=array.getJSONObject(0);  
        PatientJson patient=new PatientJson();  

        patient.setId(json.get("Id"));  
        patient.setSampleId(json.get("SampleId"));  
        patient.setGender(json.get("Gender"));  

 private String readFromFile(){
String ret = “”;

try {
InputStream inputStream = openFileInput("yourFile");

if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = “”;
StringBuilder stringBuilder = new StringBuilder();

while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}

ret = stringBuilder.toString();

inputStream.close();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, “File not found: ” + e.toString());
} catch (IOException e) {
Log.e(TAG, “Can not read file: ” + e.toString());
}
return ret;

}

"your_jsondata" 应该是什么数据类型?我需要使用流读取器(BufferedReader)读取 JSON 文件。 - Abhi.G
@user3746601,从你的JSON文件中读取所有数据并设置为字符串,然后将该字符串传递到JSONObject jsonObject=new JSONObject("your_jsondata");。 - vikas balyan

0

你需要按以下方式更改你的PatientJson

public class PatientJson {
String Id, SampleId, Gender;
//with all the three getter and setters.
}

基本上,变量的名称应与您在 JSON 中获取的名称相同,或者您必须使用 SerializedName REF 将您的变量映射到 JSON 参数,如下所示。

import com.google.gson.annotations.SerializedName;
public class PatientJson {
@SerializedName("Id")
String id;
@SerializedName("SampleId")
String sampleId;
@SerializedName("Gender")
String gender;
//with all the three getter and setters.
}

要从您的示例JSON中获取数据,您需要执行以下操作:
BufferedReader br = new BufferedReader(new FileReader(/mnt/ftp/sample.json));
  //convert the json string back to object
    Type listType = new TypeToken<ArrayList<PatientJson>>() {}.getType();
    List<PatientJson> patientList = gson.fromJson(br, listType);

我尝试过这个,但是仍然通过getter获取到null值。需要通过getter或其他方式访问这些值吗? - Abhi.G
这里的“Type”对象是什么?为了导入,我可以看到许多不同的选项。而且 jsonObject.get("Samples").getAsJsonArray() 似乎不正确。无法解析 jsonObject。 - Abhi.G
你能告诉我这里的“Type”对象是什么吗?我应该导入哪个类? - Abhi.G
Type 基本上是 java.lang.reflect.Type;https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Type.html - Manish
如果您删除上面的示例键,则代码应该可以正常工作。基本上,在您的测试文件中,这是一个PatientJson数组。 - Manish
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接