S
S
Sergey Karbivnichy2016-02-05 00:11:06
Programming
Sergey Karbivnichy, 2016-02-05 00:11:06

json deserialization in c#?

Hello! I can't figure it out with json. I tried all the examples from Google, but the examples there do not match what I need:

[
  {
    "topic": 1,
    "title": "Тест",
    "text": "Какой-то текст 1",
    "user_id": 1328,
    "user": "user 1",
    "time": 1454613213
  },
  {
    "topic": 2,
    "title": "Тест 2",
    "text": "Какой-то текст 2",
    "user_id": 1328,
    "user": "user 2",
    "time": 1454611021
  }
]

How to deserialize using newtonsoft Json.Net?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VZVZ, 2016-02-05
@hottabxp

Outside should be { } and not [ ]
I would just wrap the original string in this way before parsing:

{"response": [
  {
    "topic": 1,
    "title": "Тест",
    "text": "Какой-то текст 1",
    "user_id": 1328,
    "user": "user 1",
    "time": 1454613213
  },
  {
    "topic": 2,
    "title": "Тест 2",
    "text": "Какой-то текст 2",
    "user_id": 1328,
    "user": "user 2",
    "time": 1454611021
  }
]
}

And then everything is parsed without problems according to the sources from Google, jobj["response"] is a JArray, well, it already has JTokens / JObjects.
Although it may be possible to parse such JSON immediately into a JArray, I was not interested yet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question