2016-01-05 11 views
2

Я довольно новичок в Java, и я пытаюсь написать Iterator для своего JSON-файла. Но он продолжает говорить мне, что «несовместимые типы: JSONValue не может преобразовать в JSONObject» в JSONObject authorNode = (JSONObject) authors.next();JSONValue не может быть преобразован в JSONObject

import java.util.Iterator; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.JSONValue; 
import mende.entity.Author; 


public class JSONIteratorAuthor implements Iterator <Author> { 

    private Iterator<JSONValue> authors; 

    public JSONIteratorAuthor(JSONObject jsonObject){ 
     this.authors = ((JSONArray) jsonObject.get("authors")).iterator(); 
    } 

    @Override 
    public boolean hasNext() { 
     return this.authors.hasNext(); 
    } 

    public Author next() { 
     if(this.hasNext()){ 
      Author a = new Author(); 
      JSONObject authorNode = (JSONObject) authors.next(); 
      a.setFirstName((String) authorNode.get("first_name")); 
      a.setLastName((String) authorNode.get("last_name")); 
      return a; 
     } 
     else { 
      return null; 
     } 
    }  
} 

Может кто-нибудь мне помочь?

ответ

2

authors.next(); возвращает объект типа JSONValue

private Iterator<JSONValue> authors; 

Но вы пытаетесь привести его к несовместимому типу (JSONObject)

JSONObject authorNode = (JSONObject) authors.next(); 

Я предположил бы, что ваш Iterator<JSONValue> вместо этого следует Iterator<JSONObject>.

Смежные вопросы