0

I am working with some ugly Json. I believe it was converted from XML so the Json properties are not as expected.

"response": {
    "EventType": "SomeEvent",
    "data": {
        "Markets": {
            "market": [{
                "@name": "Derby Dash",
                "Players": {
                    "Player": [{
                        "@name": "Joe Soap",
                        "@value": "15",
                        "@categoryId": "1",
                        "@categoryDesc": "runner",
                        "#text": "30"
                    },
                    {
                        "@name": "Jolene Soap",
                        "@value": "15",
                        "@categoryId": "1",
                        "@categoryDesc": "runner",
                        "#text": "31"
                    },

So the first thing i do is convert the json to dynamic:

var jsonObject = JsonConvert.DeserializeObject<dynamic>(json);

I am then able to get all the data until such point where the property is prefixed with an @.

foreach (var market in jsonObject.response.data.Markerts)
{
    var marketInfo = new Market
    {
        Players = new List<PlayerInfo>(),
        Name = market. // How on earth do i get this property ????
    };
}

You will also notice another property prefixed with a #. That i also do not know how to get.

stoic
  • 4,700
  • 13
  • 58
  • 88
  • 1
    Create classes to deserialize into, annotate their properties (e.g. `[JsonProperty("#text")]`) and deserialize into that. – CodeCaster Jun 05 '18 at 14:16
  • specifically deserialising into a `dynamic` type. dynamic type is important as the json returned can have various formats for the same call. i have updated the title denoting it. this is not a duplicate. Please also read posts carefully before marking duplicates. – stoic Jun 05 '18 at 14:30
  • You're nearing a hundred questions, you should know that you should be very specific about your constraints in order to not get marked as a duplicate of a very common question. It looked like you used `dynamic` in order to be able to deserialize the JSON in the first place. Please [edit] your question to mention that the JSON itself can vary per call. – CodeCaster Jun 05 '18 at 14:45
  • It was clearly shown in the example I gave, that I deserialise into a dynamic. – stoic Jun 05 '18 at 14:48
  • Yes, and it clearly looks like that was a workaround because you couldn't get any other way of deserializing to work. – CodeCaster Jun 05 '18 at 14:49
  • please stop making assumptions and work based on what is given. – stoic Jun 05 '18 at 14:49
  • See also [Deserialize json with known and unknown fields](https://stackoverflow.com/questions/15253875/deserialize-json-with-known-and-unknown-fields) and [Dynamic object property name begins with number](https://stackoverflow.com/questions/22733398/dynamic-object-property-name-begins-with-number). – CodeCaster Jun 05 '18 at 14:51
  • I have to make assumptions because your question is incomplete. **Why** are you using `dynamic`? Because _"the Json properties are not as expected"_, according to your question. So I suggest an alternative, bypassing the need for `dynamic` altogether. If that is not an option, **mention that in your question**. – CodeCaster Jun 05 '18 at 14:52
  • please stop commenting on this question if you do not have the answer. giving reference to two other unrelated posts just to give reason for marking as a duplicate does not help. I also do not have to specify **Why** i use dynamic. the mere fact that i am trying to resolve this using `dynamic` should be sufficient to know that the problem is as said – stoic Jun 05 '18 at 14:55
  • The answers to the question titled "Dynamic object property name begins with number" answer your question _"// How on earth do i get this property ????"_. You can be angry about the dupe vote, my point is that this question has been asked and answered before in various forms, and if you don't clarify your question, my dupe vote stays. – CodeCaster Jun 05 '18 at 14:56
  • hahaha ... ok Thor. you live in your imaginary world while i figure this out in some other way then, but i would like to ask once again, that if you cant help, then stop spamming this thread. ... on clarifying.... what else would Thor like us mere mortals to do? – stoic Jun 05 '18 at 15:07

0 Answers0