gpt4 book ai didi

javascript - 如何在json中分割和创建单个对象?

转载 作者:行者123 更新时间:2023-12-01 06:16:33 27 4
gpt4 key购买 nike

以下内容

$.ajax({
url: "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
success: function(csv) {
const output = Papa.parse(csv, {
header: true, // Convert rows to Objects using headers as properties
});
if (output.data) {
console.log(output.data);
} else {
console.log(output.errors);
}
},
error: function(jqXHR, textStatus, errorThrow){
console.log(textStatus);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>

给予

[
{
"Province/State": "Anhui",
"Country/Region": "Mainland China",
"Lat": "31.8257",
"Long": "117.2264",
"1/22/20": "1",
"1/23/20": "9",
"1/24/20": "15",
"1/25/20": "39",
"1/26/20": "60",
"1/27/20": "70",
"1/28/20": "106",
"1/29/20": "152",
"1/30/20": "200",
"1/31/20": "237",
"2/1/20": "297",
"2/2/20": "340",
"2/3/20": "408",
"2/4/20": "480",
"2/5/20": "530"
},
{
"Province/State": "Beijing",
"Country/Region": "Mainland China",
"Lat": "40.1824",
"Long": "116.4142",
"1/22/20": "14",
"1/23/20": "22",
"1/24/20": "36",
"1/25/20": "41",
"1/26/20": "68",
"1/27/20": "80",
"1/28/20": "91",

但是日期是我需要的单个对象,而且它旁边的数字也是,所以我需要类似的东西

{
"Province/State": "Beijing",
"Country/Region": "Mainland China",
"Lat": "40.1824",
"Long": "116.4142",
"cases": [
{
"date": "1/28/20",
"people": "91",
],
"date": "1/29/20",
"people": "99",
],
"date": "1/30/20",
"people": "101",
],
},

从字面上看,我正在寻找具有单个对象的格式正确的 json

最佳答案

您不能拥有多个具有相同名称的属性,如下所示:

{
"date": {["1/22/20", "people": "22]"},
"date": {["1/23/20", "people": "45]"}
}

另外,["people": "45"] 不是有效的 JSON。最终只有最后声明的才会存在。但你可以这样做:

{
"Province/State": "Beijing",
"Country/Region": "Mainland China",
"Lat": "40.1824",
"Long": "116.4142",
"dataset":[
{"date": "1/22/20", "people": 22},
{"date": "1/23/20", "people": 45}
]
}

$.ajax({
url: "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
success: function(csv) {
const output = Papa.parse(csv, {
header: true, // Convert rows to Objects using headers as properties
dynamicTyping: true, // Convert some fields to Numbers automatically
});
if (output.data) {
const formatted = output.data.map(area => {
const obj = { dataset: [] };

Object.keys(area).forEach(key => {
if (/^\d+\/\d+\/\d+$/.test(key)) {
obj.dataset.push({ date: key, people: area[key] });
} else {
obj[key] = area[key];
}
});

return obj;
});

document.body.innerHTML = `<pre>${JSON.stringify(formatted, 0, 2)}</pre>`;
} else {
console.log(output.errors);
}
},
error: function(jqXHR, textStatus, errorThrow){
console.log(textStatus);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>

关于javascript - 如何在json中分割和创建单个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60583604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com