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

标签 c# json c#-4.0 json.net

我是 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/

相关文章:

c# - 如何将 "a b c "修剪为 "a b c"

c# - Google Tasks API BatchRequest 触发 "Duplicate Request ID in Batch Request"

.net - 将 DataTable 序列化到 Redis 或从 Redis 反序列化的最省时的方法是什么?

entity-framework - 您可以选择实体集合的所有子集合作为单个集合吗?

wcf - C#中如何将IDataReader转换为Stream

c# - 如何将对象转换为动态类型?

c# - 如何在来自 View 的 View 值中显示标签

c# - 为什么我的 .NET 组件上的索引器并不总是可以从 VBScript 访问?

javascript - 无法通过 POST 请求将 json 数据发送到服务器

javascript - JSON.stringify 和 JSON.parse 得到不同的结果