簡體   English   中英

asp.net遍歷json數據錯誤

[英]asp.net looping through json data error

我在用這個例子:

http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing

測試一些動態JSON循環,但是我得到一個錯誤,即使我在數據中看到了我的數據屬性之一也不存在。 這是我的JSON數據:

[
{
    "$id": "1",
    "$type": "TheAPI.Models.Category, TheAPI",
    "Id": 1,
    "Name": "Marina Park",
    "Description": "Morbi elementum diam sit amet mi viverra.",
    "IsActive": true,
    "NewsCategories": [
        {
            "$id": "2",
            "$type": "TheAPI.Models.NewsCategory, TheAPI",
            "Id": 1,
            "NewsId": 2,
            "CategoryId": 1,
            "News": {
                "$id": "3",
                "$type": "TheAPI.Models.News, TheAPI",
                "Id": 2,
                "Headline": "Fake News Heading 2",
                "ShortDesc": "A short description about the news item 2.",
                "Body": "Mortgage loan rates may change daily. To ensure that you receive the rate you were quoted, you may elect to lock in your rate by paying an up-front authorization fee",
                "CreateDate": "2014-07-04T00:00:00.000",
                "PublishDate": "2014-07-04T00:00:00.000",
                "ExpireDate": "2014-12-31T00:00:00.000",
                "NewsCategories": [
                    {
                        "$ref": "2"
                    }
                ]
            },
            "Category": {
                "$ref": "1"
            }
        },
        {
            "$id": "4",
            "$type": "TheAPI.Models.NewsCategory, TheAPI",
            "Id": 5,
            "NewsId": 4,
            "CategoryId": 1,
            "News": {
                "$id": "5",
                "$type": "TheAPI.Models.News, TheAPI",
                "Id": 4,
                "Headline": "Fake News Heading 4",
                "ShortDesc": "A short description about the news item 4.",
                "Body": "Mortgage loan rates may change daily. To ensure that you receive the rate you were quoted, you may elect to lock in your rate by paying an up-front authorization fee",
                "CreateDate": "2014-07-04T00:00:00.000",
                "PublishDate": "2014-07-04T00:00:00.000",
                "ExpireDate": "2014-12-31T00:00:00.000",
                "NewsCategories": [
                    {
                        "$ref": "4"
                    }
                ]
            },
            "Category": {
                "$ref": "1"
            }
        }
    ]
}

]

這是我的.Net代碼來使用它:

    private void LoadNews()
    {
        //var url = apiurl + "/news";
        var url = apiurl + "/categories?$filter=Id eq 1&$expand=NewsCategories/News";


        var syncClient = new WebClient();
        var content = syncClient.DownloadString(url);

        //Response.Write(content);

        JArray jsonVal = JArray.Parse(content) as JArray;
        dynamic categories = jsonVal;

        foreach (dynamic category in categories)
        {
            Response.Write("Cat Name: " + category.Name + "<br>");
            foreach (dynamic newscat in category.NewsCategories)
            {
                foreach (dynamic news in newscat.News)
                {
                    Response.Write(news.Headline + "<br>");
                }
            }
        }

        //ListView1.DataSource = categories.News;
        //ListView1.DataBind();

    }

當我運行上面的代碼時,似乎每個消息的新聞屬性都沒有newscat和category那樣的所有數據屬性。 我收到以下錯誤:

System.Core.dll中發生類型'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'的異常,但未在用戶代碼中處理

附加信息:“ Newtonsoft.Json.Linq.JProperty”不包含“標題”的定義

任何幫助將不勝感激。 非常感謝你。

NewsCategory上的News屬性不是數組(只是一個對象)。 下面的.c#代碼可能比這解釋得更好...

foreach (dynamic category in categories)
{
    Response.Write("Cat Name: " + category.Name + "<br>");
    foreach (dynamic newscat in category.NewsCategories)
    {
            Response.Write(newscat.News.Headline + "<br>");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM