【问题标题】:asp.net looping through json data errorasp.net循环遍历json数据错误
【发布时间】:2014-08-01 22:05:05
【问题描述】:

我用的是这个例子:

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 和类别那样的所有数据属性。我收到以下错误:

System.Core.dll 中出现“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理

附加信息:“Newtonsoft.Json.Linq.JProperty”不包含“标题”的定义

任何帮助将不胜感激。非常感谢。

【问题讨论】:

    标签: asp.net json dynamic foreach


    【解决方案1】:

    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>");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多