【问题标题】:Change structure of JSON改变 JSON 的结构
【发布时间】:2019-06-27 03:32:40
【问题描述】:

我想将数据库中的数据绑定到折线图, 但我很难制作 JSON 格式。 请帮我像这样更改 JSON:

{
  "LineListChart": [
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 14
    },
    {
      "name": "Pembunuhan",
      "data": 4
    },
    {
      "name": "Pembunuhan",
      "data": 10
    }
   ]
}

变成这样:

{
  "LineListChart": [
    {
      "name": "Pembunuhan",
      "data": [0,0,14,4,10]
    }
   ]
}

【问题讨论】:

    标签: javascript c# asp.net asp.net-mvc json.net


    【解决方案1】:

    你可以使用reduce 往下看

    var data ={
      "LineListChart": [
        {
          "name": "Pembunuhan",
          "data": 0
        },
        {
          "name": "Pembunuhan",
          "data": 0
        },
        {
          "name": "Pembunuhan",
          "data": 14
        },
        {
          "name": "Pembunuhan",
          "data": 4
        },
        {
          "name": "Pembunuhan",
          "data": 10
        }
       ]
    }
    
    var chartData= data.LineListChart.reduce((result, item)=> {
    if (!result.LineListChart){
         result.LineListChart =[{name: item.name, data: [item.data]}];
         }else result.LineListChart[0].data.push(item.data);
        
        return result;
    }, {})
    
    console.log(chartData)

    【讨论】:

      【解决方案2】:

      使用 Json.Net 的 LINQ-to-JSON API,您可以像这样转换您的 JSON:

      var obj = JObject.Parse(json);
      obj["LineListChart"] = new JArray(
          obj["LineListChart"]
          .Children<JObject>()
          .GroupBy(jo => (string)jo["name"], jo => jo["data"])
          .Select(g => new JObject(
              new JProperty("name", g.Key),
              new JProperty("data", new JArray(g))
          ))
      );
      json = obj.ToString();
      

      小提琴:https://dotnetfiddle.net/qpuQy7

      【讨论】:

        【解决方案3】:

        因为问题是json.net,所以我用C#用json.net来回答

            string test = @"{
                        'LineListChart': [
                        {
                            'name': 'Pembunuhan',
                            'data': 0
                        },
                        {
                            'name': 'Pembunuhan',
                            'data': 0
                        },
                        {
                            'name': 'Pembunuhan',
                            'data': 14
                        },
                        {
                            'name': 'Pembunuhan',
                            'data': 4
                        },
                        {
                            'name': 'Pembunuhan',
                            'data': 10
                        }
                        ]
                    }";
        
           JObject obj = JObject.Parse(test);
        
           JArray categories = (JArray)obj["LineListChart"];
        
           var query = from c in categories group c by c["name"]
                        into g select new { name = g.Key.ToString(), 
                             data =g.Select(c => (string)c["data"]) };
        
           var res = JsonConvert.SerializeObject(new { LineListChart = query.ToList() });
        

        【讨论】:

        • 关闭,但您的代码将 data 转换为逗号分隔的字符串,而不是像 OP 想要的数组。
        • 我的意思是这一行:data = string.Join(",", g.Select(c =&gt; (string)c["data"]))。我认为应该是:data = g.Select(c =&gt; c["data"])。 (不过,您已经进行的修复很好。)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-13
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        • 2015-07-08
        相关资源
        最近更新 更多