asp.net循环通过json数据错误

时间:2014-08-01 22:05:05

标签: asp.net json dynamic foreach

我正在使用这个例子:

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'不包含'标题'的定义

非常感谢任何帮助。非常感谢你。

1 个答案:

答案 0 :(得分:0)

News上的NewsCategory属性不是数组(只是一个对象)。下面的.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>");
    }
}