如何在c#中使用JSON.Net从JSON获取属性

本文关键字:JSON Net 获取 属性 | 更新日期: 2023-09-27 17:50:16

我需要解析以下json并获得所有值。我似乎不能得到任何东西,但系统。nullreferenceexception。我有另一个Searcher类来处理Jsonstring。

JSON字符串

{
    "Results": [
        {
            "Subdivision": null,
            "SchoolDistrict": null,
            "MlsArea": null,
            "ListingType": null,
            "OfficeMLSCode": "RMLSFL",
            "NotesToStaff": "",
            "ShowingInstructions": "",
            "TempNoteToStaff": "",
            "TempNoteToStaffExpiry": "/Date(-62135596800000)/",
            "Agents": [
                {
                    "FirstName": "Warren",
                    "LastName": "Tessler",
                    "AgentMlsId": "20130130001129078644000000",
                    "MlsCode": "RMLSFL",
                    "Role": 1,
                    "IsPrimary": true,
                    "Phones": [
                        {
                            "Phone": "(312) 568-8028"
                        }
                    ]
                }
            ],
            "Contacts": [],
            "RecordStatus": "1",
            "CustomerIds": [
                120204
            ],
            "ApptCenters": [
                89916
            ],
            "FriendlyMlses": [
                "AAOR",
                "BCMLS",
                "DBAAR",
                "FCAOR",
                "FLL",
                "GAN",
                "HER",
                "HLN",
                "INSP-APP",
                "MARCOMUL",
                "MFR",
                "MRT",
                "NEF",
                "NFL",
                "NON-MLS",
                "SASJ",
                "SEF",
                "SUNSHINE",
                "VER",
                "RMLSFL"
            ],
            "HasListingContract": true,
            "HasSalesContract": false,
            "IsActive": true,
            "IsPending": false,
            "IsPendingBackupContract": false,
            "ApptType": "6",
            "No3rdPartyAppts": false,
            "CanAppraise": false,
            "CanInspect": false,
            "UnmatchedPhones": [],
            "ListingId": 19758498,
            "MLSListingId": "R9978682",
            "MLSCode": "RMLSFL",
            "MLSName": "Regional MLS of Florida",
            "OfficeMLSId": "20130129223934949378000000",
            "StreetNumber": null,
            "StreetDirection": null,
            "StreetName": "3611 NW 21ST ST",
            "StreetSuffix": null,
            "Unit": null,
            "City": null,
            "State": "FL",
            "ZipCode": "33066",
            "ListingPrice": "1890",
            "Wizardized": false,
            "IsShowable": true,
            "MLSStatus": "ACT",
            "InHouseStatus": null,
            "Latitude": 26.2547,
            "Longitude": -80.17402
        }
    ],
    "TotalResults": 1,
    "MilliSeconds": 80
}        

这是我目前为止写的:

class Properties
{
    Searcher searcher = new Searcher();
    WebClient wc = new WebClient();
    public JObject parsedStr {get; set;}
    public void test()
    {
        var json = wc.DownloadString(searcher.JsonString);
        parsedStr = JObject.Parse(json);
        foreach (JToken child in parsedStr.Children())
        {
            var prop = child as JProperty;
            Console.WriteLine(prop);
        }        
    }
}

如何在c#中使用JSON.Net从JSON获取属性

如果你只是想用原始值输出属性(即不是数组或嵌套对象):

        var query = parsedStr.Descendants().OfType<JProperty>().Where(p => p.Value.Type != JTokenType.Array && p.Value.Type != JTokenType.Object);
        foreach (var property in query)
            Console.WriteLine(property);

如果您想输出所有属性,甚至那些值是数组或嵌套对象的属性,请删除Where子句。