将字符串转换为dart中的map

时间:2018-04-04 12:57:37

标签: dart flutter

我想将字符串转换为地图。

date_id|Date      |is_holiday
------------------------------
221    |01-01-2011|  
222    |02-02-2011| 
223    |03-01-2011|
224    |04-01-2011|    

String value = "{first_name : fname,last_name : lname,gender : male, location : { state : state, country : country, place : place} }"

如何将字符串转换为Map = { first_name : fname, last_name : lname, gender : male, location = { state : state, country : country, place : place } } ,其中值包含string,int,object和boolean?

我想将字符串保存到文件中并从文件中获取数据。

4 个答案:

答案 0 :(得分:12)

那是不可能的。

如果您可以将字符串更改为有效的JSON,则可以使用

          public string makeclassandfunctions(string tablename)
    {
        DataSet ds = loadtabledata(tablename);
        //the class starts frome here
        string classoftable = "</br></br></br>public class   " + tablename + "</br>{   " + "</br>";

        for (int a = 0; a <= ds.Tables[0].Columns.Count-1 ; a++)
        {
            if (ds.Tables[0].Columns[a].ColumnName.Contains("ID"))
            {
                classoftable += "public Int32 " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
            }
            else
            {
                classoftable += "public string   " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
            }

        }
        //class is ending here its variables are defined here

        classoftable += "} ";
        //the insertion function starts frome here 
        classoftable += "</br>public void insertinto" + tablename + "(" + tablename + "  obj" + tablename + ")</br>{ </br> ";
        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" insert into " + tablename + " values " + "(";
        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            if (a == ds.Tables[0].Columns.Count - 1)
            {
                classoftable += "@" + ds.Tables[0].Columns[a].ColumnName;
            }
            else
            {
                classoftable += "@" + ds.Tables[0].Columns[a].ColumnName + " , ";
            }

        }
        classoftable += ")" + @""",sqlcon);</br>";

        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
        }
        classoftable += " cmd.ExecuteNonQuery();</br>sqlcon.Close();";
        classoftable += "}</br></br>";
        //insert function ends here

        //delete function startsfrome here

        classoftable += "public void deletefrom" + tablename + "(" + "Int32 id" + ")" + "</br>{ </br> ";

        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" delete  from [" + tablename + "]" + "  where " + ds.Tables[0].Columns[0].ColumnName + "= " + @"'" + @"""" + "+id+" + @"""" + @"'" + @"""" + ",sqlcon);" + "</br> cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br></br>";


        //delete ends here

        //updatefunctionstarts frome here
        classoftable += "public void update" + tablename + "(" + tablename + "  obj" + tablename + " )</br>{";

        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" update  [" + tablename + "] set  ";

        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            string columnname = ds.Tables[0].Columns[a].ColumnName;

            if (a == ds.Tables[0].Columns.Count - 1)
            {
                classoftable += columnname + " = @" + columnname + "  ";

                classoftable += "where  " + ds.Tables[0].Columns[0].ColumnName + " = @" + ds.Tables[0].Columns[0].ColumnName;
            }
            else
            {
                classoftable += columnname + " = @" + columnname + " , ";
            }
        }
        classoftable += @"""" + ",sqlcon);</br>";

        for (int a = 0; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
        }
        classoftable += "cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br>";


        //updatesstatementfunction ends here





        return classoftable;
    }

字符串需要看起来像

import 'dart:convert';
...
Map valueMap = json.decode(value);

答案 1 :(得分:4)

您将不得不更改创建字符串的方式。

我猜您正在使用yourMap.toString()方法创建字符串,但是您应该使用json.encode(yourMap),它将您的地图解析为有效的JSON,您可以使用json.decode(yourString)来读取方法。

答案 2 :(得分:3)

创建两个对象

class User {
  final String firstName;
  final String lastName;
  final String gender;
  final location;

  User({
    this.firstName,
    this.lastName,
    this.gender,
    this.location,
  });

  User.fromJson(Map json)
      : firstName = json['firstName'],
        lastName = json['lastName'],
        gender = json['gender'],
        location = Location.fromJson(json['location']);
}

class Location {
  final String state;
  final String country;
  final String place;

  Location({
    this.state,
    this.country,
    this.place,
  });

  Location.fromJson(Map json)
      : state = json['state'],
        country = json['country'],
        place = json['place'];
}

然后像这样使用它

var user = User.fromJson(value);
print(user.firstName);

或将其转换为此列表

var user = User.fromJson(value).toList();

答案 3 :(得分:2)

在定义方法fromMaptoMap的位置输入wrapper class