如何解析JSON中的<p> </p>

时间:2015-03-31 15:32:55

标签: json

我在解释JSON脚本的第11行有一个解析错误。

我的JSON就是这样:

{
    "Subject": "S001",
    "Topic":"T001",
    "SubTopic": "ST001",
    "Questions": [
    {
        "QuestionType": 1,
        "QuestionNumber": 1,
        "DifficultyLevel": "Hard",
        "QuestionVersion": 1,
        "Question": "<p>
                    The graph below shows the mass of four children.
                    <img src=\"Image1.png\"> </img>
                    If Hakim’s weight is 40kg, who weighs 20% less than Hakim?
                    </p>",
        "Options": [
                        {
                            "Answer": "Priscilla",
                            "Index" : "0"
                        }
                        {
                            "Answer": "Janet",
                            "Index" : "1"
                        }
                        {
                            "Answer": "Noel",
                            "Index" : "2"
                        }
                        {
                            "Answer": "Andrew",
                            "Index" : "3"
                        }
                   ],
        "CorrectAnswer": "Janet",
        "Explanation": "<p>
                    20% of 40kg 
                    20/100 x 40 = 8kg
                    40kg – 8kg = 32 kg
                    </p>"
    }
    ]
}

当我在http://jsonlint.com/解析时,它会给我一个错误:

Parse error on line 11:
...        "Question": "\<p\>                   The grap
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

如何解决此错误?我需要在字符串中包含

标记。

2 个答案:

答案 0 :(得分:3)

JSON不喜欢你的换行符。那,你在选项中的对象之间缺少一些逗号。这很好用:

{
    "Subject": "S001",
    "Topic": "T001",
    "SubTopic": "ST001",
    "Questions": [
        {
            "QuestionType": 1,
            "QuestionNumber": 1,
            "DifficultyLevel": "Hard",
            "QuestionVersion": 1,
            "Question": "<p>The graph below shows the mass of four children.<img src=\"Image1.png\"> </img>If Hakim’s weight is 40kg, who weighs 20% less than Hakim?</p>",
            "Options": [
                {
                    "Answer": "Priscilla",
                    "Index": "0"
                },
                {
                    "Answer": "Janet",
                    "Index": "1"
                },
                {
                    "Answer": "Noel",
                    "Index": "2"
                },
                {
                    "Answer": "Andrew",
                    "Index": "3"
                }
            ],
            "CorrectAnswer": "Janet",
            "Explanation": "<p>20% of 40kg 20/100 x 40 = 8kg40kg – 8kg = 32 kg</p>"
        }
    ]
}

答案 1 :(得分:3)

问题不在于<p>,而在于换行符。 JSON中的字符串值不能是多行的。另见:Multiline strings in JSON