0

I would like to know if there is a quicker and direct way to read and write in C# ASP NET Core without having to create classes of JSON data models.

Consider the following JSON data received from an API. Is there a way to read fields as strings directly without having to deserialize them using JsonSerializer?

{
  "data": [
    {
      "id": "100285832430"
    },
    {
      "id": "102602230220"
    }
  ],
  "paging": {
    "cursors": {
      "before": "MTAwMTIxMDMyNDMw",
      "after": "MTAyNjY4NzAyMjIw"
    }
  }
}

Thank you

3
  • 2
    This is a string. So, what do you want to do with it? An "intermediate" solution between making Model classes and not parsing at all would be to use JObject or dynamic. But I would BenchmarkDotnet any solution against each other. Results may be surprising. Commented Dec 23, 2021 at 7:28
  • 1
    You can parse your json into dynamic without classes. Sometimes I found it useful Example Commented Dec 23, 2021 at 7:33
  • Thanks a lot. Something like dynamic is what I needed. Commented Dec 24, 2021 at 4:02

1 Answer 1

1

You can very easily copy some json text and then translate it to a model.

enter image description here

Or use something like http://www.quicktype.io

You can also use an anonymous object:

https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm

Or even a dynamic

https://stackoverflow.com/a/63457332/4122889

Btw, You can also read partial json if you're dealing with large datasets:

https://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

I gues you could also use regex to parse the string or write custom logic - but ofcourse i would not recommend that.

For example: \"id\": \"([0-9]*)\" to get all id's.

This however is not faster for development and i doubt it'll be faster for the app. Just stick with models and json. I'm not sure about your setup but perhaps there are things to automate if you have a lot of models?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I was aware of Paste Special in VS however this dynamic thing is what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.