我对json很新。我刚刚完成了关于json的研究,但我对如何控制json输出知之甚少。我做了一个使用ajax从控制器获取json数据的购物车,我试图解析输出。最终我没有得到结果。它声明未定义。
这是我的草稿
loop hypermart json element {
loop productname element{
get price based on productname + price
}
}
因此每个集市的每个集市都会有相同的产品名称和不同的价格。我怎样才能实现这种输出?我被问到了一个问题,我还没有得到答案。
这是json数据
[{
"productId":1002,
"productName":"Moghul Faiza Basmathi",
"productPic":"",
"brandName":"Faiza",
"productVolume":"5kg",
"barcode":"9555035703811",
"manufacturer":"Faiza",
"createdBy":{
"userId":2,
"username":"pak.ijan",
"password":"********",
"fullName":"Hizan Ahmad",
"sessionId":"********"},
"dateCreated":"Jul 9, 2014 3:52:08 AM",
"modifiedBy":{
"userId":2,"username":"pak.ijan",
"password":"********",
"fullName":"Hizan Ahmad",
"sessionId":"********"},
"lastModified":"Jul 9, 2014 3:52:08 AM",
"status":1,
"price":[{
"priceId":0,
"hypermart":{
"hypermartId":1,
"hypermartName":"Tesco",
"hypermartLogo":"",
"status":1},
"priceDate":"Jul 13, 2014 12:17:46 PM",
"productPrice":0.0,"status":1},
{"priceId":1,
"hypermart":{
"hypermartId":2,
"hypermartName":"Giant",
"hypermartLogo":"",
"status":1},
"priceDate":"Jul 13, 2014 12:17:46 PM",
"productPrice":0.0,"status":1},
{"priceId":2,
"hypermart":{
"hypermartId":3,
"hypermartName":"Jusco",
"hypermartLogo":"",
"status":1},
"priceDate":"Jul 13, 2014 12:17:46 PM",
"productPrice":0.0,
"status":1}],
"tag":"beras faiza basmathi moghul",
"description":"Moghul Faiza Basmathi",
"category":{
"categoryId":7,
"descEn":"Food",
"descMy":"Makanan"}
}]
答案 0 :(得分:1)
假设您从服务器获取数据字符串,例如
var myDataJson = [{
"productId":1002,
...
"price":[{
"priceId":0,
"hypermart":{
"hypermartId":1,
"hypermartName":
...
}]
现在很简单。你必须解析myDataJson
。因此,您可以立即获得真正的JavaScript对象。请记住,JSON表示JavaScript Object Notation。
因此,一种解析的现代方法是:
try {
var myCartData = JSON.parse(myDataJson);
} catch (ex) {
...
}
现在,您可以像访问其他所有Javascript对象一样访问您的真实Javascript对象:
for (key in myCartData) {
var product = myCartData[key];
var price = product['price'][0]['productPrice'];
...
}
可能您应该寻找进一步解析JSON字符串的方法,因为旧浏览器不支持JSON.parse()
。以下是许多想法:Parse JSON in JavaScript?
答案 1 :(得分:0)
逐个循环逐个元素很容易。为此你应该从头开始学习并花一些时间。
我试着为你解决。
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
var data = [
{
"productId": 1002,
"productName": "Moghul Faiza Basmathi",
"productPic": "",
"brandName": "Faiza",
"productVolume": "5kg",
"barcode": "9555035703811",
"manufacturer": "Faiza",
"createdBy": {
"userId": 2,
"username": "pak.ijan",
"password": "********",
"fullName": "Hizan Ahmad",
"sessionId": "********"
},
"dateCreated": "Jul 9, 2014 3:52:08 AM",
"modifiedBy": {
"userId": 2,
"username": "pak.ijan",
"password": "********",
"fullName": "Hizan Ahmad",
"sessionId": "********"
},
"lastModified": "Jul 9, 2014 3:52:08 AM",
"status": 1,
"price": [
{
"priceId": 0,
"hypermart": {
"hypermartId": 1,
"hypermartName": "Tesco",
"hypermartLogo": "",
"status": 1
},
"priceDate": "Jul 13, 2014 12:17:46 PM",
"productPrice": 100,
"status": 1
},
{
"priceId": 1,
"hypermart": {
"hypermartId": 2,
"hypermartName": "Giant",
"hypermartLogo": "",
"status": 1
},
"priceDate": "Jul 13, 2014 12:17:46 PM",
"productPrice": 250,
"status": 1
},
{
"priceId": 2,
"hypermart": {
"hypermartId": 3,
"hypermartName": "Jusco",
"hypermartLogo": "",
"status": 1
},
"priceDate": "Jul 13, 2014 12:17:46 PM",
"productPrice": 370,
"status": 1
}
],
"tag": "beras faiza basmathi moghul",
"description": "Moghul Faiza Basmathi",
"category": {
"categoryId": 7,
"descEn": "Food",
"descMy": "Makanan"
}
}
];
$(document).ready(function(){
var martDict = getPriceByName("Moghul Faiza Basmathi")
console.log(martDict);
var martDetail = "martDetail";
var hypermartName = "hypermartName";
var martPrice = "martPrice";
for(i in martDict){
//console.log(martDict[i]);
//console.log(martDict[i][martDetail]);
//console.log(martDict[i][martDetail][hypermartName]);
var martName = martDict[i][martDetail][hypermartName];
var price = martDict[i][martPrice]
console.log(martName+" -> "+price);
}
});
function getPriceByName(name){
var productName = "productName";
var price = "price";
var hypermart = "hypermart";
var productPrice = "productPrice";
var martDict = [];
//console.log(name);
for(i in data){
if(data[i][productName] == name){
//console.log(data[i][productName]);
var priceList = data[i][price];
//console.log(priceList);
for(j in priceList){
//console.log(priceList[j]);
//console.log(priceList[j][hypermart])
var martDetail = priceList[j][hypermart];
var martPrice = priceList[j][productPrice];
martDict.push({"martDetail":martDetail,"martPrice":martPrice});
}
}
}
return martDict;
}
</script>
如果你没有得到答案,请详细说明你的问题。
我已将所有控制台评论都放在原样,以便您可以逐个打开评论来获得更好的想法。