我工作在一個Android應用程序和我有一個JSON字符串的麻煩。我想訪問「納摩」「Docenten」下,在以下JSON:JAVA訪問json字符串
{
"Items":[
{
"Id":2157750,
"Links":[
{
"Rel":"Self",
"Href":"/api/personen/11824/afspraken/2157750"
},
{
"Rel":"Next",
"Href":"/api/personen/11824/afspraken/2157661"
}
],
"Start":"2015-11-16T07:30:00.0000000Z",
"Einde":"2015-11-16T08:20:00.0000000Z",
"LesuurVan":1,
"LesuurTotMet":1,
"DuurtHeleDag":false,
"Omschrijving":"du - rec - G3A",
"Lokatie":"103",
"Status":7,
"Type":13,
"WeergaveType":0,
"Inhoud":"<html><head></head><body style=\"font-family: 'Arial'; font-size: 12px\"><p style=\"margin: 0px 0px 10px\">lernen für Klassenarbeit,<br/>fertigmachen: 17.4</p></body></html>",
"InfoType":1,
"Aantekening":null,
"Afgerond":false,
"Vakken":[
{
"Id":3,
"Naam":"Duits"
}
],
"Docenten":[
{
"Id":706,
"Naam":"C. Reuten",
"Docentcode":"rec"
}
],
"Lokalen":[
{
"Naam":"103"
}
],
"Groepen":null,
"OpdrachtId":0,
"HeeftBijlagen":false,
"Bijlagen":null
},
...
這是我的代碼,但它給出了一個空指針錯誤:
private void handlejson_agenda(String input){
if(input != null && input != "[]" && input != "") {
try {
JSONObject jsonRootObject = new JSONObject(input);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("Items");
//Iterate the jsonArray and print the info of JSONObjects
if(jsonArray == null){
//niks
} else {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("Omschrijving").toString();
String lesuurvan = jsonObject.optString("LesuurVan").toString();
String lesuurtot = jsonObject.optString("LesuurTotMet").toString();
String lokatie = jsonObject.optString("Lokatie").toString();
String status = jsonObject.optString("Status").toString();
String datumvan = jsonObject.optString("Start").toString();
String datumtot = jsonObject.optString("Einde").toString();
String leraar = jsonObject.optJSONArray("Docenten").optString("Naam").toString();
String inhoudles = jsonObject.optString("Inhoud").toString();
String datumvanconverted = datumvan.substring(0, 10);
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
...
一切正常,除了「Leraar」字符串, 我希望有一個人可以幫助我。
-Z3r0byte
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) –
'NullPointer'?這是值得懷疑的,考慮到你的代碼不能編譯。 – njzk2