以xamarin形式反序列化数据集

时间:2018-05-23 12:15:32

标签: json xamarin.forms

如何解密/反序列化数据集。这是我从Web服务获得的。我是xamarin表格的新手。提前致谢。我试过这个json2csharp.com进行转换,但是因为它可以将数据表转换为csharp而不是数据集而出现错误。

    [  
   [  
      {  
         "bit_HasError":false,
         "vchar_ErrorMsg":""
      }
   ],
   [  
      {  
         "int_SurveyQuestionID":1,
         "vchar_Description":"we",
         "vchar_Instruction":"Question Instruction",
         "int_AnswerType":1
      },
      {  
         "int_SurveyQuestionID":5,
         "vchar_Description":"this is the question 2",
         "vchar_Instruction":null,
         "int_AnswerType":2
      }
   ],
   [  
      {  
         "int_SurveyQuestionID":1,
         "vchar_Option":"option1"
      },
      {  
         "int_SurveyQuestionID":5,
         "vchar_Option":"answer1"
      },
      {  
         "int_SurveyQuestionID":5,
         "vchar_Option":"answer2"
      },
      {  
         "int_SurveyQuestionID":5,
         "vchar_Option":"answer3"
      },
      {  
         "int_SurveyQuestionID":1,
         "vchar_Option":"optionn2"
      }
   ]
]

1 个答案:

答案 0 :(得分:1)

使用https://app.quicktype.io/它很容易上手,只需将json粘贴到那里,结果就是:

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var prject = Prject.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Prject
    {
        [JsonProperty("bit_HasError", NullValueHandling = NullValueHandling.Ignore)]
        public bool? BitHasError { get; set; }

        [JsonProperty("vchar_ErrorMsg", NullValueHandling = NullValueHandling.Ignore)]
        public string VcharErrorMsg { get; set; }

        [JsonProperty("int_SurveyQuestionID", NullValueHandling = NullValueHandling.Ignore)]
        public long? IntSurveyQuestionId { get; set; }

        [JsonProperty("vchar_Description", NullValueHandling = NullValueHandling.Ignore)]
        public string VcharDescription { get; set; }

        [JsonProperty("vchar_Instruction")]
        public string VcharInstruction { get; set; }

        [JsonProperty("int_AnswerType", NullValueHandling = NullValueHandling.Ignore)]
        public long? IntAnswerType { get; set; }

        [JsonProperty("vchar_Option", NullValueHandling = NullValueHandling.Ignore)]
        public string VcharOption { get; set; }
    }

    public partial class Prject
    {
        public static List<List<Prject>> FromJson(string json) => JsonConvert.DeserializeObject<List<List<Prject>>>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this List<List<Prject>> self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

P.S。:请注意,您必须修改类名。