skip to Main Content

I have a JSON

jsonData

{
    "data": {
      "splashPage": {
        "title": "Splash"
      },
      "homePage": {
        "title": "Home"
      }
    }
}
List<String> accessField = ['data','splashPage']; 
final out = accessField.map((e) => "['$e']").join();
Map jsonMapData = jsonDecode(jsonData); 
Map<String, dynamic> splashPageJson = '${jsonMapData}$out' as Map<String, dynamic>; 
print(splashPageJson);

I got an error can’t access to splashPage.

_CastError (type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast)

How can I access to splashPage from JSON?

Note: accessField is dynamic value

If I want to access splashPage, declaration

    accessField = ['data','splashPage'];

If I want to access homePage, declaration

    accessField = ['data','homePage'];

4

Answers


  1. Is this what you want?

    var jsonData = {
        "data": {
          "splashPage": {
            "title": "Splash"
          },
          "homePage": {
            "title": "Home"
          }
        }
    }
    
    Map jsonMapData = jsonDecode(jsonData); 
    
    List<String> accessField = ['data','splashPage'];
    
    Map<String, dynamic> requiredResult = jsonMapData[accessField[0]][accessField[1]];
    
    Login or Signup to reply.
  2. Here’s the solution:

    First import:

    import 'dart:convert';
    

    To store JSON into the map:

    final Map<String, dynamic> map = json.decode('{"data":{"splashPage":{"title":"Splash"},"homePage":{"title":"Home"}}}');
    

    To print your requirement:

    print(map["data"]["splashPage"]["title"]);
    
    Login or Signup to reply.
  3. Code for your Model:

    class Model {
      Model({
        this.data,
      });
    
      Model.fromJson(Map<String, dynamic> json) {
        data = json["data"] != null ? Data.fromJson(json["data"]) : null;
      }
      Data? data;
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> map = <String, dynamic>{};
        if (data != null) {
          map["data"] = data?.toJson();
        }
        return map;
      }
    }
    
    class Data {
      Data({
        this.splashPage,
        this.homePage,
      });
    
      Data.fromJson(Map<String, dynamic> json) {
        splashPage = json["splashPage"] != null
            ? SplashPage.fromJson(json["splashPage"])
            : null;
        homePage =
            json["homePage"] != null ? HomePage.fromJson(json["homePage"]) : null;
      }
      SplashPage? splashPage;
      HomePage? homePage;
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> map = <String, dynamic>{};
        if (splashPage != null) {
          map["splashPage"] = splashPage?.toJson();
        }
        if (homePage != null) {
          map["homePage"] = homePage?.toJson();
        }
        return map;
      }
    }
    
    class HomePage {
      HomePage({
        this.title,
      });
    
      HomePage.fromJson(Map<String, dynamic> json) {
        title = json["title"];
      }
      String? title;
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> map = <String, dynamic>{};
        map["title"] = title;
        return map;
      }
    }
    
    class SplashPage {
      SplashPage({
        this.title,
      });
    
      SplashPage.fromJson(Map<String, dynamic> json) {
        title = json["title"];
      }
      String? title;
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> map = <String, dynamic>{};
        map["title"] = title;
        return map;
      }
    }
    

    Code for your usage:

    final Model model = Model.fromJson(
        json.decode(
          '{"data":{"splashPage":{"title":"Splash"},"homePage":{"title":"Home"}}}',
        ),
      );
    
    
    print(model.data?.splashPage?.title ?? "");
    print(model.data?.homePage?.title ?? "");
    

    Don’t forgot to import:

    import 'dart:convert';
    
    Login or Signup to reply.
    • This is a question about conversion of Json data format to native data model. If you publish the Json data earlier, the problem may not be so complicated
    • try this.
      void test() {
        var json =
            '{"data":{"splashPage":{"title":"Splash"},"homePage":{"title":"Home"}}}';
        var map = jsonDecode(json) as Map<String, dynamic>;
        var model = DataResponseModel.fromJson(map);
        pr(model.data?.homePage?.title); // Home
        pr(model.data?.splashPage?.title); // Splash
      }
    
    
    
    class TitleModel {
      String? title;
      TitleModel({required this.title});
      factory TitleModel.fromJson(Map<String, dynamic> map) =>
          TitleModel(title: map['title']);
    }
    
    class DataModel {
      TitleModel? splashPage;
      TitleModel? homePage;
      DataModel({required this.splashPage, this.homePage});
      factory DataModel.fromJson(Map<String, dynamic> map) => DataModel(
            splashPage: TitleModel.fromJson(map['splashPage']),
            homePage: TitleModel.fromJson(map['homePage']),
          );
    }
    
    class DataResponseModel {
      DataModel? data;
      DataResponseModel({required this.data});
      factory DataResponseModel.fromJson(Map<String, dynamic> map) =>
          DataResponseModel(
            data: DataModel.fromJson(map['data']),
          );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search