Json strings in Flutter have two structures: Map and List.

1. Map Structure The most common basic Json structure, starting with curly braces “{}”, is called Map structure.

{
    "id" : "10086",
    "name" : "Jack",
    "phone" : "13311112222"
}
Copy the code

2. The List structure starts with square brackets “[]” and is an array in Android native.

[
    "student1",
    "student2",
    "student3"
]
Copy the code

For the above two structures, when we parse the Json string, we pass the Json string into the method as a parameter to parse. Then, what parameter format does the distribution of the two structures correspond to?

In Flutter, the parameter format of Map structure is “Map<String, dynamic> Map”. Through the Map structure, String keys can be mapped to dynamic values. So in the Map, the key value format locates String; The Value can be a String, an Int, or a custom entity type.

There is no array type in Flutter, but a List. The corresponding argument format is “List List”. The structure generated by square brackets is a List structure, and the object type inside the Flutter is also dynamic.

1. Pure Map structure

{"id" : "10086", "name" : "Jack", "phone" : "13311112222" }
Copy the code

For the above structure, we build its entity class and create a constructor for its Factory modifier:

class Student {
    String stuId;
    String stuName;
    String stuTel;

    Student({this.stuId, this.stuName, this.stuTel});

    factory Student.fromJson(Map<String, dynamic> json) {
        return Student(
            stuId : json["id"],
            stuName : json["name"],
            stuTel : json["phone"]
        )
    };
}
Copy the code

2. Map contains a List structure

{
    "id" : "10086",
    "students" : [
        "student1", 
        "student2", 
        "student3"
    ]
}
Copy the code

Entity class:

class A {
    String id;
    List<String> students;

    A({this.id, this.students});

    factory A.fromJson(Map<String, dynamic> json) {
        return A(
            id : json["id"],
            students : json["students"]
        )
    };
}
Copy the code

Type ‘List’ is not a subtype of type ‘List’; type ‘List’ is not a subtype of type ‘List’; We want to request a List, but we get a List, the program can not recognize the type, so we need to manually convert the type;

var str = json["students"];
List<String> strList = new List<String>.from(str);
students: strList;
Copy the code

Create a List that contains the data you just fetched. Change dynamic to String.

3. Nesting of Map structures

{
    "id" : "10086",
    "student" : {
        "name" : "Jack",
        "phone" : "111111"
    }
}
Copy the code

Student = Student; Student = Student; Student = Student; Type ‘_InternalLinkedHashMap<String, dynamic>’ is not a subtype of type ‘student’ ‘ We can’t directly Map the Map structure in the Map to the Student object, so we need to do a conversion:

student : Student.fromJson(json["student"]);
Copy the code

Student = student; student = student; student = student; student = Student; student = Student;

4. A List structure containing a Map is nested in a Map

{
    "id" : "10086",
    "student" : [
        {
            "name" : "Jack",
            "phone" : "111111"
        },
        {
            "name" : "Tom",
            "phone" : "222222"
        }
    ]
}
Copy the code

Class A = students; class A = Students; class A = Students; class A = Students; class A = Students;

var list = json["students"] as List;
List<Student> stus = list.map((i) => Student.fromJson(i)).toList;
Copy the code

Print (list.runtimeType); print(list.runtimeType); print(list.runtimeType); print(list.runtimeType); Go through the list and map each object in the list to the corresponding Student object;

Corresponding entity class:

class A {
    String id;
    List<Student> students;

    A({this.id, this.students});

    factory A.fromJson(Map<String, dynamic> json) {
        var list = json["students"] as List;
        List<Student> stus = list.map((i) => Student.fromJson(i)).toList();
        return A(
            id : json["id"],
            students : stus
        )
    };
}
Copy the code

5. Map is nested in List

[
    {
        "name" : "Jack",
        "phone" : "111111"
    },
    {
        "name" : "Tom",
        "phone" : "222222"
    }
]
Copy the code

Student is still the same here, but this whole thing is a List object, so create a StudentList object with the member variables List Students; But in the fromJson method for StudentList, instead of a Map, the fromJson method for StudentList is a List.

factory StudentList.fromJson(List<dynamic> json) {
    List<Student> stus = new List<Student>();
    stus = json.map((i) => Student.fromJson(i)).toList();
    return StudentList(
        students : stus
    );
}
Copy the code