简体   繁体   中英

c# : Deserealization to DataSet

Can anyone help with JSON string parse on C#?

I tried to reproduce the sample from https://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm

string json = @"{
                'Table1': [
                      {
                       'id': 0,
                       'item': 'item 0'
                      },
                      {
                        'id': 1,
                        'item': 'item 1'
                      }
                      ]
                     }";

dataSet = JsonConvert.DeserializeObject<DataSet>(json);

I receive the exception:

JsonConvert.DeserializeObject exception System.Runtime.Serialization.SerializationException: Member 'XmlSchema' was not found. 09-17 07:41:27.075 I/mono-stdout( 3691): JsonConvert.DeserializeObject exception System.Runtime.Serialization.SerializationException: Member 'XmlSchema' was not found. at System.Runtime.Serialization.SerializationInfo.GetElement (System.String name, System.Type& foundType) [0x00020] in <3fd174ff54b146228c505f23cf75ce71>:0 at System.Runtime.Serialization.SerializationInfo.GetValue (System.String name, System.Type type) [0x0002e] in <3fd174ff54b146228c505f23cf75ce71>:0 at System.Data.DataSet.DeserializeDataSetSchema (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Data.SerializationFormat remotingFormat, System.Data.SchemaSerializationMode schemaSerializationMode) [0x000f5] in :0 at System.Data.DataSet.DeserializeDataSet (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Data.SerializationFormat remotingFormat, System.Data.SchemaSerializationMode schemaSerializationMode) [0x00000] in :0 at System.Data.DataSet..ctor (System.Runtime.Serialization.SerializationInfo inf o, System.Runtime.Serialization.StreamingContext context, System.Boolean ConstructSchema) [0x00069] in :0 at System.Data.DataSet..ctor (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) [0x00000] in :0 at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,object[]) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializable (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonISerializableContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x0015b] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonCo ntainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00347] in :0 at Ne wtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0006d] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in :0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00053] in : 0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x 0002d] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in :0

What is missing? Should I add any XML schema?

As @Willem noted in comment use double quotation (this validates here ):

{
    "Table1": [{
            "id": 0,
            "item": "item 0"
        },
        {
            "id": 1,
            "item": "item 1"
        }
    ]
}

Following notation diagram is the description from JSON.org 在此处输入图片说明

In my case, I'm using Asp.Net Core. I need to use nuget and get a latest version of Newtonsoft.Json. Maybe the application use pre-install old version of Newtonsoft.Json. After get the latest Newtonsoft.Json (v12.0.1), it works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM