c# - 如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符

我是 JSON 新手。在我的 asp.net 应用程序中,我想解析 json 字符串。所以,我使用 Newtonsoft.Json 包来读取和写入 json 数据。现在,我可以解析简单的 json 数据.但是现在我收到了一些复杂的json数据进行解析。所以,我有点感动。

这是 JSON 数据:

{
    quizlist: [
     {
            QUIZ: {
                'QPROP': [
                    {
                        'name': 'FB',
                        'intro': '',
                        'timeopen': '1347871440',
                        'timeclose': '1355733840',
                        'timelimit': '0',
                        'noofques': '5',
                        'QUESTION': {
                            'QUEPROP': [
                                {
                                    'questiontext': 'Scienceisbasedont',
                                    'penalty': '0.3333333',
                                    'qtype': 'shortanswer',
                                    'answer': 'cause-and-effect',
                                    'mark'  : '5',
                                    'hint': ''
                                },
                                {
                                    'questiontext': 'otherscientistsevaluateit',
                                    'penalty': '0.3333333',
                                    'qtype': 'shortanswer',
                                    'answer': 'Peerreview',
                                    'mark'  : '5',
                                    'hint': ''
                                },
                                {
                                    'questiontext': 'Watchingavariety',
                                    'penalty': '0.3333333',
                                    'qtype': 'shortanswer',
                                    'answer': 'inductive',
                                    'mark'  : '5',
                                    'hint': ''
                                },
                                {
                                    'questiontext': 'coveriesorideas',
                                    'penalty': '0.3333333',
                                    'qtype': 'shortanswer',
                                    'answer': 'paradigmshift',
                                    'mark'  : '5',
                                    'hint': ''
                                },
                                {
                                    'questiontext': 'proportions',
                                    'penalty': '0.3333333',
                                    'qtype': 'shortanswer',
                                    'answer': 'fixed',
                                    'mark'  : '5',
                                    'hint': ''
                                }
                            ]
                        }
                    }
                ]
            }
        }
     ]
}

这是我的 C# 代码:

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);

            foreach (var data in dynObj.quizlist)
            {
                foreach (var data1 in data.QUIZ.QPROP)
                {
                    Response.Write("Name" + ":" + data1.name + "<br>");
                    Response.Write("Intro" + ":" + data1.intro + "<br>");
                    Response.Write("Timeopen" + ":" + data1.timeopen + "<br>");
                    Response.Write("Timeclose" + ":" + data1.timeclose + "<br>");
                    Response.Write("Timelimit" + ":" + data1.timelimit + "<br>");
                    Response.Write("Noofques" + ":" + data1.noofques + "<br>");
                }
              }

我可以解析直到 QPROP 数组对象中的 noofques 对象。现在还必须解析 data.QUIZ.QPROP.QUESTION.QUEPROP 数组对象...

但我未能完全解析...

请指导我摆脱这个问题...

最佳答案

foreach (var data in dynObj.quizlist)
{
    foreach (var data1 in data.QUIZ.QPROP)
    {
        Response.Write("Name" + ":" + data1.name + "<br>");
        Response.Write("Intro" + ":" + data1.intro + "<br>");
        Response.Write("Timeopen" + ":" + data1.timeopen + "<br>");
        Response.Write("Timeclose" + ":" + data1.timeclose + "<br>");
        Response.Write("Timelimit" + ":" + data1.timelimit + "<br>");
        Response.Write("Noofques" + ":" + data1.noofques + "<br>");

        foreach (var queprop in data1.QUESTION.QUEPROP)
        {
            Response.Write("Questiontext" + ":" + queprop.questiontext  + "<br>");
            Response.Write("Mark" + ":" + queprop.mark  + "<br>");
        }
    }
}

关于c# - 如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13839865/

相关文章:

java - 单元测试 Jersey Restful Services

java - 仅当不为空或不为空时 Gson 序列化字段

javascript - 在 JavaScript 中将 JSON 对象转换为 CSV 格式

json - 如何重用 Jersey 的 JSON/JAXB 进行序列化?

javascript - 在 HTML 数据属性上添加 JSON 是不是很糟糕?

php - 通过 JSON 发送 HTML 代码

jquery - 通过 jQuery 将 JSON 数据发布到 ASP .NET MVC 4 Con

java - Gson - 自动引号(“)转义?

javascript - 在javascript中将json对象写入文本文件

xml - JSON Schema 与 XML Schema 的比较及其 future