2016-11-07 186 views
1

我想知道我怎样才能得到JSON响应的总和:总和嵌套的对象属性的

我想获得的交付总和所以这将是3435 + 20

{[ 
    { 
    "date": "2016-10-01", 
    "stats": [ 
     { 
     "type": "subuser", 
     "name": "[email protected]", 
     "metrics": { 
      "blocks": 23, 
      "bounce_drops": 164, 
      "bounces": 19, 
      "clicks": 0, 
      "deferred": 412, 
      "delivered": 3435, 
      "invalid_emails": 27, 
      "opens": 0, 
      "processed": 3481, 
      "requests": 3675, 
      "spam_report_drops": 3, 
      "spam_reports": 0, 
      "unique_clicks": 0, 
      "unique_opens": 0, 
      "unsubscribe_drops": 0, 
      "unsubscribes": 0 
     } 
     } 
    ] 
    }, 
    { 
    "date": "2016-10-02", 
    "stats": [ 
     { 
     "type": "subuser", 
     "name": "[email protected]", 
     "metrics": { 
      "blocks": 0, 
      "bounce_drops": 0, 
      "bounces": 0, 
      "clicks": 0, 
      "deferred": 95, 
      "delivered": 20, 
      "invalid_emails": 0, 
      "opens": 0, 
      "processed": 0, 
      "requests": 0, 
      "spam_report_drops": 0, 
      "spam_reports": 0, 
      "unique_clicks": 0, 
      "unique_opens": 0, 
      "unsubscribe_drops": 0, 
      "unsubscribes": 0 
     } 
     } 
    ] 
    } 
]} 

我会做这样的事吗?我曾尝试这样做,但它不工作,我会得到错误:

Additional information: Cannot perform runtime binding on a null reference

 string getresponse; 

     getresponse = response.Body.ReadAsStringAsync().Result; 

     string s = getresponse; 

     dynamic o = JsonConvert.DeserializeObject(s); 

     textBox1.Text = o[0].stats[0].metrics[0].delivered; 
+1

'的问题o [0]'没有'metrics'属性。再看看你的结构。事实上,你甚至没有有效的JSON开始。 –

+0

@MattBurland不会是o [0] .stats [0] .metrics [0] .delivered; ?? –

回答

1

metrics属性是一个字典,这就是为什么你需要访问它通过一个关键的值。

dynamic result = JsonConvert.DeserializeObject<dynamic>(jsonString); 
var res = result[0].stats[0].metrics["delivered"]; 

完成编辑:您必须创建类来正确反序列化该json。

public class Rootobject 
    { 
     public string date { get; set; } 
     public Stat[] stats { get; set; } 
    } 

    public class Stat 
    { 
     public string type { get; set; } 
     public string name { get; set; } 
     public Metrics metrics { get; set; } 
    } 

    public class Metrics 
    { 
     public int blocks { get; set; } 
     public int bounce_drops { get; set; } 
     public int bounces { get; set; } 
     public int clicks { get; set; } 
     public int deferred { get; set; } 
     public int delivered { get; set; } 
     public int invalid_emails { get; set; } 
     public int opens { get; set; } 
     public int processed { get; set; } 
     public int requests { get; set; } 
     public int spam_report_drops { get; set; } 
     public int spam_reports { get; set; } 
     public int unique_clicks { get; set; } 
     public int unique_opens { get; set; } 
     public int unsubscribe_drops { get; set; } 
     public int unsubscribes { get; set; } 
    } 

在此之后,你可以得到这样的总和:

var result = JsonConvert.DeserializeObject<List<Rootobject>>(jsonString); 
var sum = result.SelectMany(x => x.stats).Sum(x => x.metrics.delivered); 
+0

这是否也是他们? –

+0

对不起,没有看到,我会很快编辑我的答案 –

+0

@JDoe。那应该是吧 –

0
public class Metrics 
    { 
     public int delivered { get; set; } 
    } 

    public class Stat 
    { 
     public Metrics metrics { get; set; } 
    } 

    public class RootObject 
    { 
     public List<Stat> stats { get; set; } 
    } 

..

 List<RootObject> o = JsonConvert.DeserializeObject<List<RootObject>>(json); 
     var result = o.Sum(x => x.stats.Sum(y => y.metrics.delivered));