0
다소 복잡한 json 문자열을 읽으려고하고 중첩 된 항목 및이를 검색하는 방법에 문제가 있습니다. 내가 널 포인트 예외 여기Java에서 Json을 변환 할 때 문제가 발생했습니다.
json으로는 더 읽기보기 여기
{
"Patient": {
"Name": {
"Given": "FirstName",
"Family": "LastName"
},
"Gender": "Female",
"DOB": "1980-07-04T00:00:00.0000000",
"AgeInYears": 36,
"MartialStatus": "Single",
"Race": "Race",
"Ethnicity": "Ethnicity",
"Class": "Inpatient",
"Address": {
"StreetAddress": "StreetAddress",
"City": "City",
"State": "State",
"ZipCode": "ZipCode",
"Country": "Country"
}
}
}
에를 얻을 중 하나 접근 방식을 실행하면
내 자바 코드는 다음
String longJson = "{'Patient': {'Name': {'Given': 'FirstName','Family': 'LastName'},'Gender': 'Female','DOB': '1980-07-04T00:00:00.0000000','AgeInYears': 36,'MartialStatus': 'Single', 'Race': 'Race','Ethnicity': 'Ethnicity','Class': 'Inpatient','Address': {'StreetAddress': 'StreetAddress','City': 'City','State': 'State','ZipCode': 'ZipCode', 'Country': 'Country'}}}";
Gson gson = new Gson();
PrescriptionReq sample = null;
sample = gson.fromJson(longJson, PrescriptionReq.class);
String firstName = sample.getPatient().getName().getGiven();
//String firstName = sample.patient.name.getGiven();
System.out.println("Testing: "+ firstName);
처럼 보인다 내 수업 :
public class PrescriptionReq {
private Patient patient;
public Patient getPatient(){
return patient;
}
public class Patient {
Name name;
Address address;
public Name getName(){
return name;
}
//Other variables
}
public class Name {
private String Given;
private String Family;
public String getGiven() {
return Given;
}
public String getFamily() {
return Family;
}
}
}
json을 잘못 저장했거나 잘못 가져 왔는지 확실하지 않습니다. 어떤 도움을 많이 주시면 감사하겠습니다!
null 부분은 무엇입니까? – SLaks
문자열 firstName = sample.getPatient(). getName(). getGiven(); 은 널 포인터 예외를 반환하고 있습니다. – mmm2893