ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Flutter에서 List 안의 List를 어떻게 파싱할 수 있습니까?
    카테고리 없음 2020. 8. 12. 16:34

    질문

    "indice"를 파싱할 수 있지만 "capitulos"를 파싱할 수 있기 때문에이 json을 파싱하는 데에 문제가 있습니다. 오류가 발생합니다. 예외가 발생했습니다. _TypeError ( 'String'유형은 'Map'유형의 하위 유형이 아님)목록 안의 목록을 어떻게 파싱할 수 있습니까? 나는 Pooja Bhaumik의 훌륭한 기사를 읽었지만 id는이 예를 숨기지 않습니다.

    [
        {
            "id": "1",
            "title": "title",
            "origem": "origin",
            "tipo": "type",
            "indice": [
                {
                    "id": 1,
                    "title": "title 1"
                },
                {
                    "id": 2,
                    "title": "title 2"
                },
            ],
            "capitulos": [
                [
                    "chapter 1 text 1",
                    "chapter 1 text 2",
                    "chapter 1 text 3"
                ],
                [
                    "chapter 2 text 1",
                    "chapter 2 text 2",
                    "chapter 2 text 3"
                ],
            ]        
        }
    ]

    내 모델 :

    class DocumentosList {
      final List<Documento> documentos;
    
      DocumentosList({
        this.documentos,
      });
    
      factory DocumentosList.fromJson(List<dynamic> parsedJson) {
        List<Documento> documentos = new List<Documento>();
        documentos = parsedJson.map((i) => Documento.fromJson(i)).toList();
    
        return new DocumentosList(documentos: documentos);
      }
    }
    
    class Documento {
      String id;
      String title;
      String origem;
      String tipo;
      List<IndiceItem> indice;
      List<Capitulos> capitulos;
    
      Documento({
        this.id,
        this.title,
        this.origem,
        this.tipo,
        this.indice,
        this.capitulos,
      });
    
      factory Documento.fromJson(Map<String, dynamic> parsedJson) {
        var list = parsedJson['indice'] as List;
        List<IndiceItem> indice = list.map((i) => IndiceItem.fromJson(i)).toList();
    
        var listCapitulos = parsedJson['capitulos'];
        List<Capitulos> capitulos =
            listCapitulos.map((i) => Capitulos.fromJson(i)).toList();
    
        return new Documento(
          id: parsedJson['id'],
          title: parsedJson['title'],
          origem: parsedJson['origem'],
          indice: indice,
          capitulos: capitulos,
        );
      }
    }
    
    class IndiceItem {
      String id;
      String title;
    
      IndiceItem({
        this.id,
        this.title,
      });
    
      factory IndiceItem.fromJson(Map<String, dynamic> parsedJson) {
        return IndiceItem(
          id: parsedJson['id'].toString(),
          title: parsedJson['title'],
        );
      }
    }
    
    class Capitulos {
      final List<Capitulo> capitulos;
    
      Capitulos({this.capitulos});
    
      factory Capitulos.fromJson(List<dynamic> parsedJson) {
        List<Capitulo> capitulos = new List<Capitulo>();
        capitulos = parsedJson.map((i) => Capitulo.fromJson(i)).toList();
    
        return new Capitulos(capitulos: capitulos);
      }
    }
    
    class Capitulo {
      final List<String> paragrafos;
    
      Capitulo({this.paragrafos});
    
      factory Capitulo.fromJson(Map<String, dynamic> parsedJson) {
        var paragrafosFromJson = parsedJson['paragrafos'];
        List<String> paragrafosList = paragrafosFromJson.cast<String>();
    
        return new Capitulo(
          paragrafos: paragrafosList,
        );
      }
    }

     

    답변1

    여기서 특별한 것은 없습니다. 내부 목록에는 문자열 유형이 있으므로

    List<List<String>> capitulos

    fromJson 및 toJson에 대해서도 필요한 변경을해야합니다.



     

     

     

     

    출처 : https://stackoverflow.com/questions/60231323/how-can-i-parse-a-list-inside-a-list-in-flutter

    댓글

Designed by Tistory.