motkot
@motkot
Программирование C#.

Почему json не парсится?

Хочу получить это значение в JSON, но выдает ошибку. Что не так?

Использую Newtonsoft

JSON:

620cdad5a670c792818897.png

Ошибка:

JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 456.


Код:

var json = JsonConvert.SerializeObject(result);

        var title = JObject.Parse(json)[0]["metadata"]["title"]; // если удалить [0], то не помогает

        print(title);
  • Вопрос задан
  • 559 просмотров
Решения вопроса 1
twobomb
@twobomb
dynamic d = JObject.Parse(json);
print(d.metadata.title);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
yarosroman
@yarosroman Куратор тега C#
C# the best
public class Metadata
    {
        public string title { get; set; }
        public string description { get; set; }
        public string media { get; set; }
        public object media_hash { get; set; }
        public int copies { get; set; }
        public object issued_at { get; set; }
        public object expires_at { get; set; }
        public object starts_at { get; set; }
        public object updated_at { get; set; }
        public object extra { get; set; }
        public object reference { get; set; }
        public object reference_hash { get; set; }
    }

    public class ApprovedAccountIds
    {
    }

    public class Root
    {
        public string token_id { get; set; }
        public string owner_id { get; set; }
        public Metadata metadata { get; set; }
        public ApprovedAccountIds approved_account_ids { get; set; }
    }


var myJsonObject = JsonConvert.DeserializeObject<List<Root>>(myJsonString);


Типа так.

https://www.newtonsoft.com/json/help/html/Deserial...

Почитайте документацию хотя бы.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы