使用.replace的自定义json值给出未定义的输出

时间:2018-03-01 06:36:30

标签: jquery json

我有这样的json:

var data = {
"items":[
{
"id":"asd01231987391",
"sku":"ABC-0128712",
"product":"product abc",
"desc":"this is abc product",
"price":1200,
"qty":1
}
]
}1400;

我使用替换编辑/自定义'1400'值为“总计”:1400,所以我可以从2行json获得值,这个代码为白色:

var Total = data+"]]";
var resTotal = Total.replace("}]}", '}],"total":');
var resTotal2 = resTotal.replace("]]", "}");
var resTotal1 = resTotal2; 


console.log(resTotal1.items);
console.log(resTotal1.total);

但是2控制台的输出仍然会给出“未经分类”的结果。

最终结果必须是这样的: 来自控制台1 = resTotal1.items:

var res = resTotal1.items;
    $.each(res, function(i, item) {

    ... loop the json from items ..
                                }
来自控制台2的

: 它仅用于显示1400;

任何人都可以制止我。

这是我的jsfiddle

JSON CUSTOM

感谢

3 个答案:

答案 0 :(得分:2)

我认为这是无效的JSON格式。无论如何,我认为这将有所帮助。

var data = '{"items":[{"id":"asd01231987391","sku":"ABC-0128712","product":"product abc","desc":"this is abc product","price":1200,"qty":1}]}1400';

    var Total = data+"]]";
    var resTotal = Total.replace("}]}", '}],"total":');
    var resTotal2 = resTotal.replace("]]", "}");
    var resTotal1 = resTotal2; 

    // Use JSON.parse();

    console.log(JSON.parse(resTotal1).total);
    console.log(JSON.parse(resTotal1).items);

答案 1 :(得分:1)

您的JSON格式错误

var data = {
        "items":[
            {
                "id":"asd01231987391",
                "sku":"ABC-0128712",
                "product":"product abc",
                "desc":"this is abc product",
                "price":1200,
                "qty":1
            }
        ],
        "total": 1400
    };

希望这将是你的json数据。要获取项目,请使用data.items并使用data.total



var data = {
        "items":[
            {
                "id":"asd01231987391",
                "sku":"ABC-0128712",
                "product":"product abc",
                "desc":"this is abc product",
                "price":1200,
                "qty":1
            }
        ],
        "total": 1400
    };

    console.log("Items...");
    console.log(data.items);
    console.log("Total...");
    console.log(data.total);




更新:(基于评论)

如果您的输入是字符串并且格式与您定义的一样,那么首先使其成为有效的json字符串然后解码为有效的json对象



var data =  '{"items":[{"id":"asd01231987391","sku":"ABC-0128712","product":"product abc","desc":"this is abc product","price":1200,"qty":1}]}1400';

var formattedJSON = data.replace("}]}", "}], \"total\":")+"}";
data = JSON.parse(formattedJSON);

console.log("Items...");
console.log(data.items);
console.log("Total...");
console.log(data.total);




答案 2 :(得分:0)

在字符串替换之前,您必须先将data转换为字符串,然后将其转换回JSON。

var Total = data.toString()+"]]";
var resTotal = Total.replace("}]}", '}],"total":');
var resTotal2 = resTotal.replace("]]", "}");
var resTotal1 = resTotal2; 
var resTotal_final = JSON.parse(resTotal1);


$(".resx").append('<span>'+resTotal_final.total+'</span>' );

var data = '{"items":[{"id":"asd01231987391","sku":"ABC-0128712","product":"product abc","desc":"this is abc product","price":1200,"qty":1}]}1400';

var Total = data.toString() + "]]";
var resTotal = Total.replace("}]}", '}],"total":');
var resTotal2 = resTotal.replace("]]", "}");
var resTotal1 = resTotal2;
var resTotal_final = JSON.parse(resTotal1);

$(".resx").append('<span>' + resTotal_final.total + '</span>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="resx">
</div>

https://jsfiddle.net/1asyozcf/3/