【问题标题】:parse json file using gson/jackson java API使用 gson/jackson java API 解析 json 文件
【发布时间】:2015-07-29 10:46:16
【问题描述】:

我是 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 课程
  • @user3746601 我不太确定,但请检查字符大小写。即如果字符串是id 确保JSON 也是id 而不是Id
  • 谁能帮我解决这个问题。使用 gson 还是 jackson API?

标签: java json jackson gson


【解决方案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 文件。
  • @user3746601 ,从您的 JSON 文件中读取所有数据并设置为字符串,然后将该字符串传递给 JSONObject jsonObject=new JSONObject("your_jsondata");
【解决方案2】:

您需要如下更改您的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 获得空值。我是否需要通过 getter 或其他方式访问这些值?
  • 这里的“类型”对象是什么?要导入,我可以看到许多不同的选项。而且 jsonObject.get("Samples").getAsJsonArray() 似乎也不正确。 jsonObject 无法解析。
  • 你能告诉我这里的“Type”对象是什么吗?我应该导入哪个类?
  • Type 基本上是java.lang.reflect.Type; docs.oracle.com/javase/7/docs/api/java/lang/reflect/Type.html
  • 耐心列表为空。我认为 PatientJson 类结构仍然不正确。
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多