Flutter 解析嵌套对象列表

huangapple go评论63阅读模式
英文:

Flutter parsed nested list of object

问题

如何解析此JSON响应

{
    "name_of_data_dummy_one": false,
    "name_of_data_dummy_two": "1",
    "name_of_data_dummy_three": 1,
    "name_of_data": [
        {
            "name_of_data_one": "bla bla bla",
            "name_of_data_two": "bla bla bla",
            "name_of_data_three": "bla bla bla",
            "name_of_data_four": "bla bla bla",
            "name_of_data_five": "bla bla bla",
            "name_of_data_six": "bla bla bla",
            "name_of_data_seven": "bla bla bla",
            "name_of_data_eight": "bla bla bla",
            "name_of_data_nine": "bla bla bla",
            "name_of_data_ten": "bla bla bla"
        },
        {
            "name_of_data_one": "bla bla bla",
            "name_of_data_two": "bla bla bla",
            "name_of_data_three": "bla bla bla",
            "name_of_data_four": "bla bla bla",
            "name_of_data_five": "bla bla bla",
            "name_of_data_six": "bla bla bla",
            "name_of_data_seven": "bla bla bla",
            "name_of_data_eight": "bla bla bla",
            "name_of_data_nine": "bla bla bla",
            "name_of_data_ten": "bla bla bla"
        }
    ]
}

这是用于访问API的代码

Future<Either<String, List<CompanyListResponseModel>>> getCompanyList(
      GetListCompanyModel companyListModel) async {
    final response = await http.post(
      Uri.parse('URL'),
      body: thisIsModel.toJson(),
    );

    if (response.statusCode == 200) {
     return Right(List<CompanyListResponseModel>.from(jsonDecode(response.body)
        .map((x) => CompanyListResponseModel.fromJson(x))));
    } else {
      return const Left('get product error');
    }
  }

我已经生成了类似 quicktype.io 示例的响应数据,如下所示

class CompanyListResponseModel {
    bool? nameOfDataDummyOne;
    String? nameOfDataDummyTwo;
    int? nameOfDataDummyThree;
    List<NameOfDatum>? nameOfData;

    CompanyListResponseModel({
        this.nameOfDataDummyOne,
        this.nameOfDataDummyTwo,
        this.nameOfDataDummyThree,
        this.nameOfData,
    });

    factory CompanyListResponseModel.fromRawJson(String str) => 
    CompanyListResponseModel.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory CompanyListResponseModel.fromJson(Map<String, dynamic> json) => 
    CompanyListResponseModel(
        nameOfDataDummyOne: json["name_of_data_dummy_one"],
        nameOfDataDummyTwo: json["name_of_data_dummy_two"],
        nameOfDataDummyThree: json["name_of_data_dummy_three"],
        nameOfData: json["name_of_data"] == null ? [] : 
        List<NameOfDatum>.from(json["name_of_data"].map((x) => 
        NameOfDatum.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "name_of_data_dummy_one": nameOfDataDummyOne,
        "name_of_data_dummy_two": nameOfDataDummyTwo,
        "name_of_data_dummy_three": nameOfDataDummyThree,
        "name_of_data": nameOfData == null ? [] : 
        List<dynamic>.from(nameOfData!.map((x) => x.toJson())),
    };
}

class NameOfDatum {
    String? nameOfDataOne;
    String? nameOfDataTwo;
    String? nameOfDataThree;
    String? nameOfDataFour;
    String? nameOfDataFive;
    String? nameOfDataSix;
    String? nameOfDataSeven;
    String? nameOfDataEight;
    String? nameOfDataNine;
    String? nameOfDataTen;

    NameOfDatum({
        this.nameOfDataOne,
        this.nameOfDataTwo,
        this.nameOfDataThree,
        this.nameOfDataFour,
        this.nameOfDataFive,
        this.nameOfDataSix,
        this.nameOfDataSeven,
        this.nameOfDataEight,
        this.nameOfDataNine,
        this.nameOfDataTen,
    });

    factory NameOfDatum.fromRawJson(String str) => 
    NameOfDatum.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory NameOfDatum.fromJson(Map<String, dynamic> json) => 
    NameOfDatum(
        nameOfDataOne: json["name_of_data_one"],
        nameOfDataTwo: json["name_of_data_two"],
        nameOfDataThree: json["name_of_data_three"],
        nameOfDataFour: json["name_of_data_four"],
        nameOfDataFive: json["name_of_data_five"],
        nameOfDataSix: json["name_of_data_six"],
        nameOfDataSeven: json["name_of_data_seven"],
        nameOfDataEight: json["name_of_data_eight"],
        nameOfDataNine: json["name_of_data_nine"],
        nameOfDataTen: json["name_of_data_ten"],
    );

    Map<String, dynamic> toJson() => {
        "name_of_data_one": nameOfDataOne,
        "name_of_data_two": nameOfDataTwo,
        "name_of_data_three": nameOfDataThree,
        "name_of_data_four": nameOfDataFour,
        "name_of_data_five": nameOfDataFive,
        "name_of_data_six": nameOfDataSix,
        "name_of_data_seven": nameOfDataSeven,
        "name_of_data_eight": nameOfDataEight,
        "name_of_data_nine": nameOfDataNine,
        "name_of_data_ten": nameOfDataTen,
      };
}

我遇到了这个错误 "Unhandled Exception: type 'List' is not a subtype of type 'String' in type cast"

我的期望是创建一个ListView,按照示例中的方式解析数据,顺便说一下,我使用了bloc状态管理。

英文:

How to parsed this response of json

{
&quot;name_of_data_dummy_one&quot;: false,
&quot;name_of_data_dummy_two&quot;: &quot;1&quot;,
&quot;name_of_data_dummy_three&quot;: 1,
&quot;name_of_data&quot;: [
{
&quot;name_of_data_one&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_two&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_three&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_four&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_five&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_six&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_seven&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_eight&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_nine&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_ten&quot;: &quot;bla bla bla&quot;
},
{
&quot;name_of_data_one&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_two&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_three&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_four&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_five&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_six&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_seven&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_eight&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_nine&quot;: &quot;bla bla bla&quot;,
&quot;name_of_data_ten&quot;: &quot;bla bla bla&quot;
}
]
}

And heres the code for access the api

Future&lt;Either&lt;String, List&lt;CompanyListResponseModel&gt;&gt;&gt; getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse(&#39;URL&#39;),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return Right(List&lt;CompanyListResponseModel&gt;.from(jsonDecode(response.body)
.map((x) =&gt; CompanyListResponseModel.fromJson(x))));
} else {
return const Left(&#39;get product error&#39;);
}
}

I have generated response data like the example in quicktype.io loks like this

class CompanyListResponseModel {
bool? nameOfDataDummyOne;
String? nameOfDataDummyTwo;
int? nameOfDataDummyThree;
List&lt;NameOfDatum&gt;? nameOfData;
CompanyListResponseModel({
this.nameOfDataDummyOne,
this.nameOfDataDummyTwo,
this.nameOfDataDummyThree,
this.nameOfData,
});
factory CompanyListResponseModel.fromRawJson(String str) =&gt; 
CompanyListResponseModel.fromJson(json.decode(str));
String toRawJson() =&gt; json.encode(toJson());
factory CompanyListResponseModel.fromJson(Map&lt;String, dynamic&gt; 
json) =&gt; CompanyListResponseModel(
nameOfDataDummyOne: json[&quot;name_of_data_dummy_one&quot;],
nameOfDataDummyTwo: json[&quot;name_of_data_dummy_two&quot;],
nameOfDataDummyThree: json[&quot;name_of_data_dummy_three&quot;],
nameOfData: json[&quot;name_of_data&quot;] == null ? [] : 
List&lt;NameOfDatum&gt;.from(json[&quot;name_of_data&quot;]!.map((x) =&gt; 
NameOfDatum.fromJson(x))),
);
Map&lt;String, dynamic&gt; toJson() =&gt; {
&quot;name_of_data_dummy_one&quot;: nameOfDataDummyOne,
&quot;name_of_data_dummy_two&quot;: nameOfDataDummyTwo,
&quot;name_of_data_dummy_three&quot;: nameOfDataDummyThree,
&quot;name_of_data&quot;: nameOfData == null ? [] : 
List&lt;dynamic&gt;.from(nameOfData!.map((x) =&gt; x.toJson())),
};
}
class NameOfDatum {
String? nameOfDataOne;
String? nameOfDataTwo;
String? nameOfDataThree;
String? nameOfDataFour;
String? nameOfDataFive;
String? nameOfDataSix;
String? nameOfDataSeven;
String? nameOfDataEight;
String? nameOfDataNine;
String? nameOfDataTen;
NameOfDatum({
this.nameOfDataOne,
this.nameOfDataTwo,
this.nameOfDataThree,
this.nameOfDataFour,
this.nameOfDataFive,
this.nameOfDataSix,
this.nameOfDataSeven,
this.nameOfDataEight,
this.nameOfDataNine,
this.nameOfDataTen,
});
factory NameOfDatum.fromRawJson(String str) =&gt; 
NameOfDatum.fromJson(json.decode(str));
String toRawJson() =&gt; json.encode(toJson());
factory NameOfDatum.fromJson(Map&lt;String, dynamic&gt; json) =&gt; 
NameOfDatum(
nameOfDataOne: json[&quot;name_of_data_one&quot;],
nameOfDataTwo: json[&quot;name_of_data_two&quot;],
nameOfDataThree: json[&quot;name_of_data_three&quot;],
nameOfDataFour: json[&quot;name_of_data_four&quot;],
nameOfDataFive: json[&quot;name_of_data_five&quot;],
nameOfDataSix: json[&quot;name_of_data_six&quot;],
nameOfDataSeven: json[&quot;name_of_data_seven&quot;],
nameOfDataEight: json[&quot;name_of_data_eight&quot;],
nameOfDataNine: json[&quot;name_of_data_nine&quot;],
nameOfDataTen: json[&quot;name_of_data_ten&quot;],
);
Map&lt;String, dynamic&gt; toJson() =&gt; {
&quot;name_of_data_one&quot;: nameOfDataOne,
&quot;name_of_data_two&quot;: nameOfDataTwo,
&quot;name_of_data_three&quot;: nameOfDataThree,
&quot;name_of_data_four&quot;: nameOfDataFour,
&quot;name_of_data_five&quot;: nameOfDataFive,
&quot;name_of_data_six&quot;: nameOfDataSix,
&quot;name_of_data_seven&quot;: nameOfDataSeven,
&quot;name_of_data_eight&quot;: nameOfDataEight,
&quot;name_of_data_nine&quot;: nameOfDataNine,
&quot;name_of_data_ten&quot;: nameOfDataTen,
};
}

and I got error like this "Unhandled Exception: type 'List < dynamic >' is not a subtype of type 'String' in type cast"

My expectation is to create a listview, constrained in parsing data as in the example, by the way I use bloc state management

答案1

得分: 1

Your response is just a single CompanyListResponseModel but you treat it as if it's a list, which it's not. Change your function to:

Future<Either<String, CompanyListResponseModel>> getCompanyList(
      GetListCompanyModel companyListModel) async {
    final response = await http.post(
      Uri.parse('URL'),
      body: thisIsModel.toJson(),
    );

    if (response.statusCode == 200) {
     return CompanyListResponseModel.fromJson(jsonDecode(response.body));
    } else {
      return const Left('get product error');
    }
}
英文:

Your response is just a single CompanyListResponseModel but you treat it as a if it's a list, which it's not. Change you function to

Future&lt;Either&lt;String, CompanyListResponseModel&gt;&gt; getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse(&#39;URL&#39;),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return CompanyListResponseModel.fromJson(jsonDecode(response.body)));
} else {
return const Left(&#39;get product error&#39;);
}
}
</details>

huangapple
  • 本文由 发表于 2023年6月15日 17:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定