Learn, Share, Build

269
20 сентября 2017, 09:39

Can anyone help with JSON string parse on c# ? I'm rather new in this area....

I try 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 <eabff56c075e40a688404208276b3098>: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 <eabff56c075e40a688404208276b3098>:0 
  at System.Data.DataSet..ctor (System.Runtime.Serialization.SerializationInfo inf
o, System.Runtime.Serialization.StreamingContext context, System.Boolean ConstructSchema) [0x00069] in <eabff56c075e40a688404208276b3098>:0 
  at System.Data.DataSet..ctor (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) [0x00000] in <eabff56c075e40a688404208276b3098>: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 <c19705166c7c4a608e182e859c4de6d2>: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 <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.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 <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00053] in <c19705166c7c4a608e182e859c4de6d2>:
0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <c19705166c7c4a608e182e859c4de6d2>:0 

What is missing ? Should I add any XML schema ? Thanks in advance

Answer 1

You should use " instead '

Check your Json text in https://jsonlint.com you have an exception like

**"Error: Parse error on line 1:
'Table1': [{        'id':"**

but if you write it so

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

than should all work.

READ ALSO
Получить ширину текста c#

Получить ширину текста c#

Моя задача это задать Label ширину точно подходящую к содержимому в этом Label если текст заранее известенАвторазмер не предлагать

352
После cancelTokenSource.Cancel() , Task остается в состоянии RanToCompletion

После cancelTokenSource.Cancel() , Task остается в состоянии RanToCompletion

Пытаюсь правильно прервать выполнение функции через token, однако task остается всегда в состоянии RanToCompletionПодскажите пожалуйста, в чем проблема?

221
Выполнение кода в блоке try/catch

Выполнение кода в блоке try/catch

Есть группа методов, выполняемых асинхронноЗапуск метода на исполнение выполняется следующим образом

214
Чтение содержимого сайта [дубликат]

Чтение содержимого сайта [дубликат]

На данный вопрос уже ответили:

246