Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - How to access JSON properties after dynamic deserialize .NET

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"
            },
            "[email protected]": "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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would need to use reflection:

foreach (var property in json.GetType().Properties)
{
    var propertyName = property.Name;
    var propertyValue = property.GetValue();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...