在Dart中序列化的JSON值不会反序列化

时间:2019-09-10 18:32:12

标签: c# .net json flutter dart

我正在尝试在Flutter(Dart)中序列化一些数据,并将其发送到我的C#.NET服务器进行处理。问题在于,在.NET中反序列化JSON数据时,它无法将某些值识别为字符串(大多数情况下无法将所述字符串解析为布尔值)。

对于Dart我已经尝试过

  1. dart:convert
  2. flutterdev建议的JSON解析方法
'@JsonSerializable(explicitToJson: true)
ClassName {
...
factory ClassName.fromJson(Map<String, dynamic> json) => _$ClassNameFromJson(json);
  Map<String, dynamic> toJson() => _$ClassNameToJson(this);
}

对于C#.NET,我已经尝试过

  1. ClassName(SerializationInfo info, StreamingContext ctx)与映射
  2. NewtonSoft.JsonConvert
  3. JavascriptSerializer

这里有一些参考资料可以帮助我将疲倦的杂物拼凑在一起。

  • 从flutter应用程序中收到的编码字符串
{
    JobComplete: false,
    PolicyCount: 50,
    Removes: [{
        Raw: false,
        HalId: Removes,
        PolicyShort: REMOVES,
        PolicyLong: REMOVESTEMPLATE,
        IsClaims: false,
        CurrentPage: null,
        TotalPages: null
    }],
    Reprints: [{
        Raw: REPRINTSTEMPLATE,
        HalId: Reprints,
        PolicyShort: REPRINTS,
        PolicyLong: REPRINTSTEMPLATE,
        IsClaims: false,
        CurrentPage: null,
        TotalPages: null
    }, {
        Raw: N50000101016912NYDB2NMZ31,
        HalId: CBP3,
        PolicyShort: 16912,
        PolicyLong: 16912 NYDB2NMZ31,
        IsClaims: true,
        CurrentPage: 1,
        TotalPages: 1
    }],
    JobMeta: [],
    JobType: CLAIMS,
    TrackingId: null,
    HalIdLong: CB0625P3CR,
    HalId: CBP3,
    PrintDate: 01 / 01,
    DueDate: 01 / 05,
    MachineInfo: {
        ShortHalId: CB,
        StufferInserter: [1, 2, 3, 4, 6],
        FeedB: true,
        POM: false,
        JobName: CB_CLAIMS_HEART LETTER,
        JobDescription: Claims Heart Correspondence Bifold,
        MachineSetup: CLASS 1,
        SET 3,
        CRN 2 D BIFOLD,
        Envelope: 23 - 1471,
        EnvelopePOM: | POM: 23 - 1472,
        Stuffers: [{
            ID: 23 - 1450,
            POMID: 01 - 0101,
            Color: Color(0xff64ff64)
        }, {
            ID: 23 - 1453,
            POMID: 01 - 0101,
            Color: Color(0xff00dcdc)
        }, {
            ID: 23 - 1450,
            POMID: 01 - 0101,
            Color: Color(0xff64ff64)
        }, {
            ID: 23 - 1451,
            POMID: 01 - 0101,
            Color: Color(0xffff6464)
        }, {
            ID: 23 - 7043,
            POMID: 01 - 0101,
            Color: Color(0xffdcdcdc)
        }],
        SpecialInstructions: Operator report must include all damaged checks.Include LISTING with POMs,
        MachineType: BIFOLD,
        PBPSCode: 11
    }
}
  • 通过重新共享https://i.gyazo.com/27969594b2d5beb4fdb58a82418133a5.png细分的错误
  • 我找到了一个解决方法,该方法是在字符串中用引号引起来,例如String str = '"PaddingFix"'
  • 另一个解决方法是重命名字符串JSON可接受的布尔值,例如:String str = 'false'

C#条码类别:


[Serializable()]
public class Barcode
{
    public string Raw { get; set; }
    public string HalId { get; set; }
    public string PolicyShort { get; set; }
    public string PolicyLong { get; set; }
    public bool IsClaims { get; set; }
    public int? CurrentPage { get; set; }
    public int? TotalPages { get; set; }
    public string readable = "NOTASSIGNED";


    public Barcode(String raw, String halId, String policyShort, String policyLong,
        bool isClaims, int currentPage, int totalPages)
    {
        Raw = raw;
        HalId = halId;
        PolicyShort = policyShort;
        PolicyLong = policyLong;
        IsClaims = isClaims;
        CurrentPage = currentPage;
        TotalPages = totalPages;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Raw", Raw);
        info.AddValue("HalId", HalId);
        info.AddValue("PolicyShort", PolicyShort);
        info.AddValue("PolicyLong", PolicyLong);
        info.AddValue("IsClaims", IsClaims);
        info.AddValue("CurrentPage", CurrentPage);
        info.AddValue("TotalPages", TotalPages);
    }

    public Barcode(SerializationInfo info, StreamingContext context)
    {
        Raw = (string)info.GetValue("Raw", typeof(string));
        HalId = (string)info.GetValue("HalId", typeof(string));
        PolicyShort = (string)info.GetValue("PolicyShort", typeof(string));
        PolicyLong = (string)info.GetValue("PolicyLong", typeof(string));
        IsClaims = (bool)info.GetValue("IsClaims", typeof(bool));
        CurrentPage = (int)info.GetValue("CurrentPage", typeof(int));
        TotalPages = (int)info.GetValue("TotalPages", typeof(int));
    }
}

最后但并非最不重要的是,我的Flutter应用程序的条形码类


@JsonSerializable(explicitToJson: true)
class Barcode {
  Barcode(this.raw, this.halId,this.policyShort,this.policyLong, this.isClaims);

  @JsonKey(name: 'Raw')
  String raw;
  @JsonKey(name: 'HalId')
  String halId;
  @JsonKey(name: 'PolicyShort')
  String policyShort;
  @JsonKey(name: 'PolicyLong')
  String policyLong;
  @JsonKey(name: 'IsClaims')
  bool isClaims;
  @JsonKey(name: 'CurrentPage')
  int currentPage;
  @JsonKey(name: 'TotalPages')
  int totalPages;

  String readable = 'NOTASSIGNED';

  factory Barcode.fromJson(Map<String, dynamic> json) => _$BarcodeFromJson(json);
  Map<String, dynamic> toJson() => _$BarcodeToJson(this);

  /*
  Barcode.fromJson(Map<String, dynamic> json)
   : raw = json['Raw'],
    halId = json['HalId'],
    policyShort = json['PolicyShort'],
    policyLong = json['PolicyLong'],
    isClaims = json['IsClaims'],
    currentPage = json['CurrentPage'],
    totalPages = json['TotalPages'];

  Map<String, dynamic> toJson() => {
    'Raw' : raw,
    'HalId' : halId,
    'PolicyShort': policyShort,
    'PolicyLong': policyLong,
    'IsClaims':isClaims,
    'CurrentPage':currentPage,
    'TotalPages':totalPages
  };*/
}

TL; DR:我有一个Flutter类,带有一个List<2ndCustomClass>。由于某种原因,在通过C#返回2ndCustomClass之后,我在解析df.loc[~df['Box'].str.contains(r'_{2}')] 时遇到了问题。我相信这是由于字符串没有包含在引号中,但无法想到一个优雅的解决方案,也看不到我的问题所在。

0 个答案:

没有答案