日期:2014-05-16  浏览次数:20416 次

json字符串解析
{"1":{"SID":"1","RDATE":"19","$ROW_NUM$":"0","$TABLE_CODE$":"MG_REMARK","RCONTENT":"223","RUSERID":"12","TYPEBY":"2","RID":"1","$PK_CODE$":"1`2`1","RSTATE":"1","RIP":"123"}},{"5":{"SID":"1","RDATE":"19","$ROW_NUM$":"1","$TABLE_CODE$":"MG_REMARK","RCONTENT":"223","RUSERID":"12","TYPEBY":"2","RID":"5","$PK_CODE$":"5`2`1","RSTATE":"1","RIP":"123"}}



这个json串怎么解析呢,比如我想获得里面的RDATE的值。(其中"1","5"都是从数据库中获取的主键它们都是变化的)

------解决方案--------------------
JSON格式有问题。。

    var d = { "1": { "SID": "1", "RDATE": "19", "$ROW_NUM$": "0", "$TABLE_CODE$": "MG_REMARK", "RCONTENT": "223", "RUSERID": "12", "TYPEBY": "2"
, "RID": "1", "$PK_CODE$": "1`2`1", "RSTATE": "1", "RIP": "123"
    }
, "5": { "SID": "1", "RDATE": "19", "$ROW_NUM$": "1", "$TABLE_CODE$": "MG_REMARK", "RCONTENT": "223", "RUSERID": "12", "TYPEBY": "2", "RID": "5", "$PK_CODE$": "5`2`1", "RSTATE": "1", "RIP": "123"}
    };
var item
for (var k in d) {
    item = d[k];

    alert(k+"\n"+item.SID)
}

------解决方案--------------------
你的json 是工具转的吗?
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


    <script type="text/javascript">

var json=[{"1":{"SID":"1","RDATE":"19","$ROW_NUM$":"0","$TABLE_CODE$":"MG_REMARK","RCONTENT":"223","RUSERID":"12","TYPEBY":"2","RID":"1","$PK_CODE$":"1`2`1","RSTATE":"1","RIP":"123"}},

{"5":{"SID":"1","RDATE":"19","$ROW_NUM$":"1","$TABLE_CODE$":"MG_REMARK","RCONTENT":"223","RUSERID":"12","TYPEBY":"2","RID":"5","$PK_CODE$":"5`2`1","RSTATE":"1","RIP":"123"}}];

for(var i in json){//第一层数组
for(var j in json[i]){//第二层数组元素
for(var k in json[i][j]){//最里层字面量
if(k=="RDATE"){//如果不判断会输出所有的属性值
document.write(json[i][j][k]+"\n");
}
}
}

}
   
    </script>
</head>
<body>

</body>
</html>