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"}]"}]

这是用于序列化数据的类,我正在使用 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

4 回答 4

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\"}]"
    }
]

您将能够将此结构映射到您当前的对象模型。但我建议您使用第一种方法。

于 2013-07-02T08:12:15.843 回答
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"
    }]
}]
于 2013-07-02T08:12:35.270 回答
0

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

public class Details
{ public int Id {get; set;} ... }
于 2013-07-02T08:13:56.577 回答
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; }
}

您可能希望为开始时间等设置正确的数据类型......

于 2013-07-02T08:16:40.713 回答