JSON匿名类型属性未定义

JSON anonymous type property undefined?

本文关键字:属性 未定义 类型 JSON      更新时间:2023-09-26

在我的mvc3项目中,我返回Json对象:

 return Json(new { ID = guid, FileName = file.FileName, FullPath = filename });

然后,在JS代码中,我尝试访问字段,例如:

            onComplete: function (event, queueId, fileObj, response, data) {
                alert(response.ID); //test
            }

但是我得到了CCD_ 1消息。如果我只得到alert(response);,我会看到有效的对象:

{"ID":"22186ea1-a56a-45d1-9d13-d19f003dedf9","FileName":"file.txt","FullPath":"some_path"}

那么如何访问这些属性呢?

您可能看到需要解析为JavaScript数据结构的JSON文本。

var parsed = JSON.parse(response);
alert( parsed.ID ); 

如果不进行解析,您将尝试访问String对象的ID属性。

var str = '{"ID":"22186ea1-a56a-45d1-9d13-d19f003dedf9","FileName":"file.txt","FullPath":"some_path"}';
alert( str.ID );  // undefined