Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。

3个月前关闭。





以下



$.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/

10-10 21:28