【问题标题】:Converting JSON array to JSON Object将 JSON 数组转换为 JSON 对象
【发布时间】:2014-03-25 09:37:40
【问题描述】:

我正在尝试将 JSON 数组转换为对象。这是一种被黑的方式,但对我的目的来说已经足够了。

基本上我正在写一个方法来得到这个

var data = [{
    "MonthYearShortName": "Sep-13",
        "TotalCustomers": 1905.0,
        "Aquisition": 317.0,
        "Attrition": 9.0
}, {
    "MonthYearShortName": "FY-14",
        "TotalCustomers": 2158.0,
        "Aquisition": 401.0,
        "Attrition": 15.0909090909091
}]

变成这样的

data = [{
    key: 'Attrition',
    color: '#d62728',
    values: [{
        "label": "Sep-13",
        "value": 9
    }, {
        "label": "FY-14",
            "value": 15.0909090909091
    }]
},

{
    key: 'Total Customer',
    color: '#1f77b4',
    values: [{
        "label": "Sep-13",
        "value": 1905
    }, {
        "label": "FY-14",
        "value": 2158
    }]
},

{
    key: 'Aquisition',
    color: '#1f7774',
    values: [{
         "label": "Sep-13",
         "value": 317
    }, {
            "label": "FY-14",
            "value": 401
    }]
}

];

颜色暂时是静态的。我稍后会处理它。

现在开始我的破解方式(我知道这很粗糙)

我尝试了这样的方法来消除损耗

 var data = @"[{""MonthYearShortName"": ""Sep-13"",""TotalCustomers"": 1905.0,""Aquisition"": 317.0,""Attrition"": 9.0}, {""MonthYearShortName"": ""FY-14"",""TotalCustomers"": 2158.0,""Aquisition"": 401.0,""Attrition"": 15.0909090909091}]";
                JArray a = JArray.Parse(data);

                var label1 = a[0]["MonthYearShortName"].ToString();
                var label2 = a[1]["MonthYearShortName"].ToString();
                var totalCustomer1 = a[0]["TotalCustomers"].ToString();
                var totalCustomer2 = a[1]["TotalCustomers"].ToString();
                var aquisition1 = a[0]["Aquisition"].ToString();
                var aquisition2 = a[1]["Aquisition"].ToString();
                var attrition1 = a[0]["Attrition"].ToString();
                var attrition2 = a[1]["Attrition"].ToString();
JObject rss1 =
                new JObject(
                    new JProperty("channel",
                        new JObject(
                            new JProperty("Key", "Attrition"),
                            new JProperty("color", "#d62728"),
                            new JProperty("values",
                                new JArray(
                                    from p in a
                                    select new JObject(
                                        new JProperty("label", a[0]["MonthYearShortName"].ToString()),
                                        new JProperty("value", attrition1),
                                        new JProperty("label", a[1]["MonthYearShortName"].ToString()),
                                        new JProperty("value", attrition2)))))));

当我尝试这个时,我得到了一个

Can not add property label to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.

现在,如果有人可以提出一种更清洁的方法(我现在想不出任何方法),我将非常感激,或者如果我的代码可以得到纠正,那将很有帮助。

谢谢

【问题讨论】:

    标签: c# json linq json.net


    【解决方案1】:

    你可以试试这样吗?

    [Test]
    public void Test()
    {
        var data = @"[
                {'MonthYearShortName': 'Sep-13','TotalCustomers': 1905.0,'Aquisition': 317.0,'Attrition': 9.0},
                {'MonthYearShortName': 'FY-14','TotalCustomers': 2158.0,'Aquisition': 401.0,'Attrition': 15.0909090909091}
                ]";
        dynamic jarr = JArray.Parse(data);
    
        var attritions = new List<ExpandoObject>();
        var aquisitions = new List<ExpandoObject>();
        var totalCustomers = new List<ExpandoObject>();
    
        foreach (dynamic o in jarr)
        {
            dynamic attr = new ExpandoObject();
            attr.label = o.MonthYearShortName;
            attr.value = o.Attrition;
    
            attritions.Add(attr);
    
            dynamic acq = new ExpandoObject();
            acq.label = o.MonthYearShortName;
            acq.value = o.Aquisition;
            aquisitions.Add(acq);
    
            dynamic cust = new ExpandoObject();
            cust.label = o.MonthYearShortName;
            cust.value = o.TotalCustomers;
            totalCustomers.Add(acq);
    
        }
    
    
        dynamic attrition = new ExpandoObject();
        dynamic aquisition = new ExpandoObject();
        dynamic totalCustomer = new ExpandoObject();
    
        attrition.Key = "Attrition";
        attrition.Color = "#d62728";
        attrition.Values = attritions;
    
        aquisition.Key = "Acquisition";
        aquisition.Color = "#1f7774";
        aquisition.Values = aquisitions;
    
        totalCustomer.Key = "Total Customer";
        totalCustomer.Color = "#1f77b4";
        totalCustomer.Values = totalCustomers;
    
        var result = new[] { attrition,totalCustomer, aquisition };
        var resultString = JsonConvert.SerializeObject(result, Formatting.Indented);
        Console.Write(resultString);
    }
    

    【讨论】:

    • 我在 `attr.label = o.MonthYearShortName;` 处收到错误 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'MonthYearShortName'
    • 你使用的类型是否完全相同,即dynamic,我也用newtonsoft 6.0.1测试过
    • 我正在尝试使用您共享的相同代码。也许我们不能那样访问 JArray?我一无所知。
    • .NET 4 或更高版本和 Newtonsoft 6.0.1 下,代码应该可以工作。您是否收到编译或运行时错误?
    • 我的错,我使用的是旧的 JSON dll
    猜你喜欢
    • 2012-07-21
    • 2020-10-02
    • 2018-10-11
    • 2018-04-16
    • 2018-09-12
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多