js怎么解析json格式字符串 js怎么判断字符串是否为json格式
js怎么解析json格式字符串
var result={"Category":[{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/yinpin.jpg"},{"categoryId":2,"categoryName":"食品","categoryImage":"/upload/shiping.jpg"},{"categoryId":3,"categoryName":"酒类","categoryImage":"/upload/jiullei.jpg"}],"recommend":{"id":11,"productName":"统一老坛泡椒牛肉袋面香辣味110g*24袋","filenameSmall":"/upload/ty_ltpj_small.jpg","productPrice":48.0,"productCost":47.5}}
var val = result.Category[0].categoryId
这样就能得到值了啊
你的代码失效的原因有两个:
1、result已经是JSON格式的数据了,所以不需要再次用JSON.parse(result)进行转换
2、result.Category是个数组,所以要用[0]来取出某一项的值
js怎么判断字符串是否为json格式
var str = "1*无sim卡或卡无效*已经连接*5*2014-01-10 09:23:46&1*无sim卡或卡无效*已经连接*5*2014-01-10 09:23:50&1*无sim卡或卡无效*已经连接*5*2014-01-10 09:23:54&"var arr = str.split("*")arr 就是数组格式的json格式json: 数组
js javascript 将一段字符串转为JSON格式,字符串转换问题
var str = "[同意,不同意]"
var arr = eval(( str ))
var newArray = []
for (var i = 0 i < arr.length i ) {
var obj = {}
obj.text = arr[i]
obj.value = arr[i]
newArray.push(obj)
}
alert(newArray)
或者
var str = ["同意","不同意"]
var arr = JSON.parse(str)
var newArray = []
for (var i = 0 i < arr.length i ) {
var obj = {}
obj.text = arr[i]
obj.value = arr[i]
newArray.push(obj)
}
alert(newArray)