N
N
Nikolay372018-11-21 00:27:26
JSON
Nikolay37, 2018-11-21 00:27:26

How to use the Newtonsoft.Json directive?

using System;
using Newtonsoft.Json;

namespace Example
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                    dynamic json = JsonConvert.DeserializeObject(e.Data); // данные получаются от WS соединения
                    //Пример данных в json - data: "{"name":"Phone","id":"2532","last_id":"88883"}"
                    Console.WriteLine(json.data); // Тут отдает - "name":"Phone","id":"2532","last_id":"88883"
                    Console.WriteLine(json.data.name); // тут выдает ошибку Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ""Newtonsoft.Json.Linq.JValue" не содержит определения для "name""
}
}
}

How to deal with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey V, 2018-11-21
@Nikolay37

//Пример данных в json - data: "{"name":"Phone","id":"2532","last_id":"88883"}"

datayou have a string in the field . do the deserialization again, but for this line
PS
is not sure, but if instead dynamicof describing the data model as a class, then perhaps Json.NET can be forced to do this automatically (using attributes on the field data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question