我无法让我的json对象正确

时间:2016-08-25 23:26:54

标签: javascript arrays json

var ppc = {
    "cpu" : [
        //intel cpu
        {"name" : "Pentium-G4400", "price" : "240", "make" : "intel", "socket" : "1151"},
        {"name" : "i3-6100", "price" : "520", "make" : "intel", "socket" : "1151"},
        {"name" : "i3-3240", "price" : "614", "make" : "intel", "socket" : "1155"},
        {"name" : "i3-6300", "price" : "745", "make" : "intel", "socket" : "1151"},
        {"name" : "i5-4460", "price" : "850", "make" : "intel", "socket" : "1150"},
        {"name" : "i5-6400", "price" : "850", "make" : "intel", "socket" : "1151"},
        {"name" : "i5-4690", "price" : "950", "make" : "intel", "socket" : "1150"},
        {"name" : "i5-6600K", "price" : "1100", "make" : "intel", "socket" : "1151"},
        {"name" : "i7-4790", "price" : "1300", "make" : "intel", "socket" : "1150"},
        {"name" : "i7-6700", "price" : "1500", "make" : "intel", "socket" : "1151"},
        {"name" : "i7-6800K", "price" : "2250", "make" : "intel", "socket" : "2011"},
        //AMD cpu
        {"name" : "A8-7650K", "price" : "495", "make" : "amd", "socket" : "FM2"},
        {"name" : "FX-8350", "price" : "800", "make" : "amd", "socket" : "AM3"},
        {"name" : "FX-8370", "price" : "1010", "make" : "amd", "socket" : "AM3"}

]};
嘿,我是一个非常新的javascript和我正在研究一个项目,我试图让这个json工作,我不能让它工作,你看到我在代码中看不到的东西? 它在原始代码上更像是“cpu”,这是我试图创建的函数:

function getRes() {
    "use strict";
    //use - 1 - gaming | 2 - video edit | 3 - music creator || 4 - photo edit | 5 - developer | 6 - simple
    var cpub, gpub, mbb, ramb, memoryb, ssdb, fcpu, fgpu, fmb, fram, fmem, fssd, bud, cp, use, s, i, pc = JSON.parse(ppc);
    cp = 1000;
    bud = document.getElementById("budget");
    //use = ((document.getElementById("use")||{}).value)||"";
    //use = $("#use option:selected").html();
//    if(use === 3 || use === 5 ||| use === 6)cpu = bud * 50 / 100;
  //  if (use === 1 || use === 2 || use === 4) {
        console.log("step 1");
        cpub = bud * 30 / 100;
        gpub = bud * 30 / 100;
        mbb = bud * 10 / 100;
        ramb = bud * 10 / 100;
        memoryb = bud * 10 / 100;
        ssdb = bud * 10 / 100;
        if (((document.getElementById("cpu-pref") || {}).value)||"" === 1) { // 1 = intel - 2 = amd
            for (i in pc.cpu)
                {
                    if (pc.cpu[i].price < cpub && pc.cpu[i].make === "intel" && (cpub - pc.cpu[i].price) < cp) {
                            cp = cpub - pc.cpu[i].price;
                            fcpu = pc.cpu[i].name;
                            s = i;
                            console.log("i =" + i);
                            console.log("fcpu =" + fcpu);
                            console.log("cp =" + cp);
                            console.log("cpu =" + cpub);
                            i = i + 1;
                        }
                }
            } else {
            for(i in pc.cpu)
                {
                    if (pc.cpu[i].price < cpub && pc.cpu[i].make === "amd" && (cpub - pc.cpu[i].price) < cp) {
                            cp = cpub - pc.cpu[i].price;
                            fcpu = pc.cpu[i].name;
                            s = i;
                            i = i + 1;
                        }
                }
            }
            bud = bud - pc.cpu[s].price;
            window.alert(fcpu);
  //  }
    return 1;
}

1 个答案:

答案 0 :(得分:1)

使用http://codebeautify.org/jsonviewerhttp://jsonlint.com/等工具,您可以检查您的json是否有效。 你不能使用评论,但在JavaScript中一切都应该没问题。 无论如何,没有评论你的json是有效的。