这个json格式是否正确?

时间:2013-07-02 08:04:02

标签: c# javascript asp.net json

我有一个json数据,当我试图解析它返回错误不正确的语法请帮我找到语法错​​误。

[{"isData":"Yes","Details":"[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00  ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]"}]

这是用于序列化数据的类,我正在使用javascript序列化器

public enum Data
{
    Yes,
    No
}

public class MessageResponse()
{
    public string isData { get; set; }
    public string Details { get; set; }
} 


      List<MessageResponse> response = new List<MessageResponse>();
string strJson="[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00  ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]";
 var newData = new MessageResponse
                            {
                                isData = Data.Yes.ToString(),
                                Details = strJson
                            };

                            response.Add(newData);

            var jsonSerialiser1 = new JavaScriptSerializer();
           string result = jsonSerialiser1.Serialize(response);

4 个答案:

答案 0 :(得分:2)

这是无效的JSON。 Details属性格式不正确。您应该删除值周围的引号。它应该是这样的:

[
    {
        "isData": "Yes",
        "Details": [
            {
                "Id": "70",
                "Name": "Test",
                "FileName": "Uploaded",
                "FileFormat": ".mp4",
                "FileType": "Video",
                "FileDuration": "00:30:00  ",
                "StartTime": "/Date(1372617000000)/",
                "EndTime": "/Date(1372681771000)/",
                "File": "2562013172331815635077778118152815.mp4"
            }
        ]
    }
]

或者如果你想要Details是一个字符串属性(代表JSON),这有点蹩脚,你应该正确地转义双引号:

[
    {
        "isData": "Yes",
        "Details": "[{\"Id\":\"70\",\"Name\":\"Test\",\"FileName\":\"Uploaded\",\"FileFormat\":\".mp4\",\"FileType\":\"Video\",\"FileDuration\":\"00: 30: 00\",\"StartTime\":\"/Date(1372617000000)/\",\"EndTime\":\"/Date(1372681771000)/\",\"File\":\"2562013172331815635077778118152815.mp4\"}]"
    }
]

您可以将此结构映射到当前对象模型。但我会建议你使用第一种方法。

答案 1 :(得分:1)

从详细资料数据中删除“

[{
    "isData":"Yes",
    "Details":
    [{
        "Id":"70",
        "Name":"Test",
        "FileName":"Uploaded",
        "FileFormat":".mp4",
        "FileType":"Video",
        "FileDuration":"00:30:00",
        "StartTime":"/Date(1372617000000)/",
        "EndTime":"/Date(1372681771000)/",
        "File":"2562013172331815635077778118152815.mp4"
    }]
}]

答案 2 :(得分:0)

详细信息应该是类型类(即用户定义的类),它应该包含所有属性。

public class Details
{ public int Id {get; set;} ... }

答案 3 :(得分:0)

首先你的json无效。

" [

之前和之后不应该有]
[
    {
        "isData": "Yes",
        "Details": [
            {
                "Id": "70",
                "Name": "Test",
                "FileName": "Uploaded",
                "FileFormat": ".mp4",
                "FileType": "Video",
                "FileDuration": "00: 30: 00",
                "StartTime": "/Date(1372617000000)/",
                "EndTime": "/Date(1372681771000)/",
                "File": "2562013172331815635077778118152815.mp4"
            }
        ]
    }
]

其次,你的课程可以改进为:

public class MessageResponse
{
    public string isData { get; set; }
    public Details Details { get; set; }
} 

public class Details
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FileName { get; set; }
    public string FileFormat { get; set; }
    public string FileType { get; set; }
    public string FileDuration { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public string File { get; set; }
}

您可能希望设置正确的数据类型,例如Start Time等...