0

I am creating C# console app where i consume API and get JSON response. How can i access JSON properties in that JSON after deserialization

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

in JS json.value or json["value"] will work but here....no

I am trying to use dynamic cuz repsonse that i am getting very very complicated. So making class and desrialize to that class nope.

{
    "@odata.context": "fff",
    "value": [
        {
            "@odata.etag": "\"3eee8ca6-7382-4760-88ec-8b759592123a,1\"",
            "createdDateTime": "2021-01-04T16:25:34Z",
            "eTag": "\"3eee8ca6-7382-4760-88ec-8b759592123a,1\"",
            "id": "1",
            "lastModifiedDateTime": "2021-01-04T16:25:34Z",
            "webUrl": "fff",
            "createdBy": {
                "user": {
                    "email": "ff",
                    "id": "2a95b5a3-c3a2-4fed-9490-d5e370fa643c",
                    "displayName": "ffff"
                }
            },
            "lastModifiedBy": {
                "user": {
                    "email": "ff",
                    "id": "2a95b5a3-c3a2-4fed-9490-d5e370fa643c",
                    "displayName": "ff"
                }
            },
            "parentReference": {
                "siteId": "dddd"
            },
            "contentType": {
                "id": "ddd",
                "name": "Item"
            },
            "fields@odata.context": "a",
            "fields": {
                "@odata.etag": "\"gg\"",
                "id": "1",
                "ContentType": "Item",
                "Title": "What is your name?",
                "Modified": "2021-01-04T16:25:34Z",
                "Created": "2021-01-04T16:25:34Z",
                "AuthorLookupId": "9",
                "EditorLookupId": "9",
                "_UIVersionString": "1.0",
                "Attachments": false,
                "Edit": "",
                "LinkTitleNoMenu": "What is your name?",
                "LinkTitle": "What is your name?",
                "ItemChildCount": "0",
                "FolderChildCount": "0",
                "_ComplianceFlags": "",
                "_ComplianceTag": "",
                "_ComplianceTagWrittenTime": "",
                "_ComplianceTagUserId": "",
                "Answer": "g"
            }
        }
    ]
}




using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker;
using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker.Models;
using Newtonsoft.Json.Linq;
Exc
  • 1,473
  • 3
  • 15
  • 30

2 Answers2

2

You would need to use reflection:

foreach (var property in json.GetType().Properties)
{
    var propertyName = property.Name;
    var propertyValue = property.GetValue();
}
Russ
  • 4,091
  • 21
  • 32
0

With a dynamic object, you can "dot" into the properties just like on a regular class, you just won't get auto-complete.

For example, on a dynamic object with a "foo" property with the value "bar" you could do var myFoo = myDynamicObj.foo.

If you wanted to discover those properties programmatically, you'd use reflection like the other answer shows, using json.GetType().Properties.

istrupin
  • 1,423
  • 16
  • 32
  • i am getting Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Text.Json.JsonElement' does not contain a definition for 'value' – Exc Jan 06 '21 at 18:10
  • please take a look at JSON that i added i want to access fields – Exc Jan 06 '21 at 18:12
  • So when you do as the type, I believe you're converting to a JObject -- what version of JSON.NET are you using (or are you using something other than JSON.Net?) – istrupin Jan 06 '21 at 18:56
  • i am using .NET CORE 3.1 do not do not which version of JSON it just poped up form intelisense – Exc Jan 06 '21 at 19:14
  • Hmm -- can you post your imported namespaces in your original question? I just want to make sure what serialization library you use. `JsonConvert` I believe is from Newtonsoft's JSON.NET, in which case you can either use ExpandoObject, or, if you're using a newer version of the library, the dynamic should still work, so I wanted to test it out and make sure we're on the same version. – istrupin Jan 06 '21 at 19:54
  • usings added plz – Exc Jan 07 '21 at 11:42