添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
任性的菠菜  ·  Python - ...·  2 月前    · 
坏坏的拐杖  ·  postgresql - Rails 5 ...·  1 年前    · 
怕考试的打火机  ·  SparkSQL ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to convert success JSON string to Object. May be I am not doing in proper manner But I have couple of question related to converting JSON to Object.

Also I am getting this exeption in Log.

NoSuchMethodError: Class '**_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments**.<br>
Receiver: _LinkedHashMap len:3
**Tried calling: cast<Map<String, dynamic>>()**
  • Should I need cast that data object in model too?
  • Which is the best and simple way to convert JSON to Object (SignUpResponse).
  • Its better to do with serialization ?
  • Response JSON :

    "data": { "_id": "5bd2a59f4c59de1ac517d7bf", "email": "fjhsd@gmail.com", "phoneNumber": "2417874147", "isAddressApproved": 0, "unitNumber": "144", "streetAddress": "sdfsddsf", "area": "asd", "zipCode": "112233", "totalOrder": 0, "todayOrder": 0, "isPauseDelivery": false, "vacationStartDt": null, "vacationEndDt": null, "defaultLiter": 1 "message": "New User Created", "error": false

    SignUpResponse which was created by online tool.

    class SignUpResponse {
        Data data;
        String message;
        bool error;
        SignUpResponse({this.data, this.message, this.error});
        SignUpResponse.fromJson(Map<String, dynamic> json) {
        data = json['data'] != null ? new Data.fromJson(json['data']) : null;
        message = json['message'];
        error = json['error'];
        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.data != null) {
            data['data'] = this.data.toJson();
        data['message'] = this.message;
        data['error'] = this.error;
        return data;
    class Data {
        String sId;
        String email;
        String phoneNumber;
        int isAddressApproved;
        String unitNumber;
        String streetAddress;
        String area;
        String zipCode;
        int totalOrder;
        int todayOrder;
        bool isPauseDelivery;
        Null vacationStartDt;
        Null vacationEndDt;
        int defaultLiter;
        Data(
            {this.sId,
            this.email,
            this.phoneNumber,
            this.isAddressApproved,
            this.unitNumber,
            this.streetAddress,
            this.area,
            this.zipCode,
            this.totalOrder,
            this.todayOrder,
            this.isPauseDelivery,
            this.vacationStartDt,
            this.vacationEndDt,
            this.defaultLiter});
        Data.fromJson(Map<String, dynamic> json) {
        sId = json['_id'];
        email = json['email'];
        phoneNumber = json['phoneNumber'];
        isAddressApproved = json['isAddressApproved'];
        unitNumber = json['unitNumber'];
        streetAddress = json['streetAddress'];
        area = json['area'];
        zipCode = json['zipCode'];
        totalOrder = json['totalOrder'];
        todayOrder = json['todayOrder'];
        isPauseDelivery = json['isPauseDelivery'];
        vacationStartDt = json['vacationStartDt'];
        vacationEndDt = json['vacationEndDt'];
        defaultLiter = json['defaultLiter'];
        Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.sId;
        data['email'] = this.email;
        data['phoneNumber'] = this.phoneNumber;
        data['isAddressApproved'] = this.isAddressApproved;
        data['unitNumber'] = this.unitNumber;
        data['streetAddress'] = this.streetAddress;
        data['area'] = this.area;
        data['zipCode'] = this.zipCode;
        data['totalOrder'] = this.totalOrder;
        data['todayOrder'] = this.todayOrder;
        data['isPauseDelivery'] = this.isPauseDelivery;
        data['vacationStartDt'] = this.vacationStartDt;
        data['vacationEndDt'] = this.vacationEndDt;
        data['defaultLiter'] = this.defaultLiter;
        return data;
    

    Calling REST POST API

    void postCallSignUp(BuildContext context, var body) {
    // This Function will check Internet is available or not.
    Utils.checkConnection().then((connectionResult) {
        if (connectionResult) {
        http.post(Constants.BASE_URL + Constants.CREATE_USER,
            body: body,
            headers: {
                "Accept": "application/json",
                "content-type": "application/json"
            }).then((http.Response response) {
            final String res = response.body;
            final int statusCode = response.statusCode;
            if (statusCode < 200 || statusCode > 400 || json == null) {
            _onFailureResponse(new Exception("Error while fetching data"));
            } else {
            //Parsing json response to particular Object.
            print(json.decode(res));
            // Unable to cast json here...
            final parsed = json.decode(res).cast<Map<String, dynamic>>();
            parsed.map<SignUpResponse>((json) => SignUpResponse.fromJson(json));
            SignUpResponse signUpResponse = parsed.map<SignUpResponse>((json) => SignUpResponse.fromJson(json));
            _onSuccessResponse(signUpResponse);
        } else {
        _onNoInternetConnection();
        Utils.showAlert(context, "Alert", "Internet is not connected.", () {
            Navigator.pop(context);
    

    Unable to understand what is happening here ? Why we have to do so much for convert our json to particular object ?

    Don't need to use cast, you can parse directly to a Map

    final Map parsed = json.decode(res); 
    

    After you have a map you can use that data to convert into your Object.

    final signUp = SignUpResponse.fromJson(parsed);
    

    And if you want to parse an array of objects, you could do something like this:

    //assuming this json returns an array of signupresponse objects
    final List parsedList = json.decode(res); 
    List<SignUpResponse> list = parsedList.map((val) =>  SignUpResponse.fromJson(val)).toList();
                    This oddly works while list = json.decode(res).map((val) => SignUpResponse.fromJson(val)).toList(); seem to fail.
    – Nae
                    Feb 13, 2019 at 14:57
           country({this.id,this.name,this.currency});
           factory country.fromJson(Map<String, dynamic> parsedJson){
             return country(
             id: parsedJson['id'].toString(),
             name : parsedJson['name'].toString(),
             currency: parsedJson['currency'].toString() 
    

    API CALL :

        await http.post("http://calikidsmap.com/test.php").then((response){
        var ddd=jsonDecode(response.body);
        Country_object_list = (ddd as List)
          .map((data) => new country.fromJson(data))
          .toList();
    

    Here is a basic answer for this if you want to access your information after the request with no extra steps.

     Map<String, dynamic> data = jsonDecode(response.body);
     String token = data["data"]["access_token"];
    

    the structure of the object from the request was like this:

    "success":1, "error":[], "data": { "access_token":"13d2ec9d1094903b1e81de8af059233e9f36ec4d", "expires_in":2628000, "token_type":"Bearer"

    Same answer but, what happens when you have nested JsonObjects? Easy:

    Given a jsonResponse like this:

    "id": 1, "type": 15, "innerClass": { "id": 1, "type": "testing"

    This is an example of how the code looks like:

    class MainClass {
      int id = 0;
      int version = 0;
      InnerClass innerClass;
      MainClass (
          this.id,
          this.version,
          this.innerClass
    //Create the same factory converter, but using another inner factory 'fromJson' converter
    //for the inner class object (InnerClass.fromJson(json['inner_class']))
      factory MainClass.fromJson(dynamic json) {
        return MainClass(
            json['id'] as int, // << put your json response keys instead.
            json['version'] as int,
            InnerClass.fromJson(json['innerClass']) // << this one
    

    Then, repeat the same strategy but for the inner class object:

    class InnerClass {
      int id = 0;
      String type = 'testing_type';
      InnerClass (
          this.id,
          this.type
      factory InnerClass.fromJson(dynamic json) {
        return InnerClass(
            json['id'] as int,
            json['type'] as String
    

    Finally, you could cosume that, in another part of you project like this:

    try {
              mainClassDartObject = MainClass.fromJson(response.body);
         } catch (e) {
              print(e);
      # Your other dev_dependencies here
      build_runner: <latest_version>
      json_serializable: <latest_version>
    

    Object class.

    import 'package:json_annotation/json_annotation.dart';
    part 'user.g.dart'; //"user" is your dart file name
    /// JSON serialization logic to be generated.
    @JsonSerializable()
    class User {
      User(this.name, this.email);
      String name;
      String email;
      factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
      Map<String, dynamic> toJson() => _$UserToJson(this);
    

    And run this in Terminal of Android Studio.

    flutter pub run build_runner build
    

    Now convert like this:

    Map results =  json.decode(response.body);
    User user = User.fromJson(results);
    

    Source: https://flutter.dev/docs/development/data-and-backend/json

    This worked for me using http: ^0.13.3 library

    LoginResponseModel posts = LoginResponseModel();
        final response = await http.post(
          Uri.parse(URL HERE),
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          encoding: Encoding.getByName('utf-8'),
          body: {
            "param1": value1,
            "param2": value2,
            "param3": value3,
            "param4": value4
        if (response.statusCode == 200) {
          // If the server did return a 200 OK response,
          // then parse the JSON.
          final Map<String, dynamic> parsed = json.decode(response.body);
          posts = LoginResponseModel.fromJson(parsed);
        } else {
          // If the server did not return a 200 OK response,
          // then throw an exception.
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.