从json字符串中获取信息?

时间:2014-01-19 12:20:43

标签: c# json

我收到了一个json字符串,我需要帮助将其映射到一个对象

目前我正在使用:

string request = webClient.DownloadString(url);

返回发票的json字符串:

"{\"invoices\":[
{\"url\":\"https://api.freeagent.com/v2/invoices/0000001\",\"contact\":\"https://api.freeagent.com/v2/contacts/000001\",\"dated_on\":\"2010-10-29\",\"due_on\":\"2010-11-28\",\"reference\":\"0001\",\"currency\":\"GBP\",\"exchange_rate\":\"1.0\",\"net_value\":\"60.0\",\"sales_tax_value\":\"68.0\",\"total_value\":\"528.0\",\"paid_value\":\"528.0\",\"due_value\":\"0.0\",\"status\":\"Paid\",\"omit_header\":false,\"payment_terms_in_days\":30,\"paid_on\":\"2011-11-14\"},{\"url\":\"https://api.freeagent.com/v2/invoices/0000002\",\"contact\":\"https://api.freeagent.com/v2/contacts/000002\",\"dated_on\":\"2010-11-19\",\"due_on\":\"2010-12-19\",\"reference\":\"0002\",\"currency\":\"GBP\",\"exchange_rate\":\"1.0\",\"net_value\":\"80.0\",\"sales_tax_value\":\"6.5\",\"total_value\":\"326.5\",\"paid_value\":\"650.0\",\"due_value\":\"76.5\",\"status\":\"Overdue\",\"comments\":\"Work\",\"omit_header\":false,\"payment_terms_in_days\":30}]}"

我有发票类:

public class Invoices
    {
        public string url { get; set; }
        public string project { get; set; } //--
        public string contact { get; set; } //--
        public string dated_on { get; set; } //--
        public string due_on { get; set; } //--
        public string paid_on { get; set; }
        public string reference { get; set; } //--
        public string currency { get; set; } //--
        public string exchange_rate { get; set; } //--
        public string net_value { get; set; }
        public string sales_tax_value { get; set; }
        public string total_value { get; set; }
        public string paid_value { get; set; }
        public string due_value { get; set; }
        public string discount_percent { get; set; } 
        public string status { get; set; }
        public string comments { get; set; } //--
        public string omit_header { get; set; }
        public string payment_terms_in_days { get; set; } //--
        public string ec_status { get; set; } //need to add fields
        public string written_off_date { get; set; }
}

不同的发票可以返回不同的信息,这就是为什么类中有很多可能不在json字符串中的值。 我尝试过这些方法,但不断出错:

List<Invoices> list = JsonConvert.DeserializeObject<List<Invoices>>(request);

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(request);

有人可以告诉我如何获取字符串中显示的信息吗?

编辑: 我也尝试过:

var info = JsonConvert.DeserializeObject(request);

并将其作为jObect返回

{
  "invoices": [
    {
      "url": "https://api.freeagent.com/v2/invoices/000000",
      "contact": "https://api.freeagent.com/v2/contacts/000000",
      "dated_on": "2010-10-29",
      "due_on": "2010-11-28",
      "reference": "0000",
      "currency": "GBP",
      "exchange_rate": "1.0",
      "net_value": "960.0",
      "sales_tax_value": "568.0",
      "total_value": "528.0",
      "paid_value": "528.0",
      "due_value": "0.0",
      "status": "Paid",
      "omit_header": false,
      "payment_terms_in_days": 30,
      "paid_on": "2011-11-14"
    },
    {
      "url": "https://api.freeagent.com/v2/invoices/000000",
      "contact": "https://api.freeagent.com/v2/contacts/000000",
      "dated_on": "2010-11-19",
      "due_on": "2010-12-19",
      "reference": "0000",
      "currency": "GBP",
      "exchange_rate": "1.0",
      "net_value": "980.0",
      "sales_tax_value": "46.5",
      "total_value": "326.5",
      "paid_value": "650.0",
      "due_value": "76.5",
      "status": "Overdue",
      "comments": "Work",
      "omit_header": false,
      "payment_terms_in_days": 30
    }
}

2 个答案:

答案 0 :(得分:2)

这些类(就像你发布的Json一样)应该是这样的:

public class Invoice
{
    public string url { get; set; }
    public string contact { get; set; }
    public string dated_on { get; set; }
    public string due_on { get; set; }
    public string reference { get; set; }
    public string currency { get; set; }
    public string exchange_rate { get; set; }
    public string net_value { get; set; }
    public string sales_tax_value { get; set; }
    public string total_value { get; set; }
    public string paid_value { get; set; }
    public string due_value { get; set; }
    public string status { get; set; }
    public bool omit_header { get; set; }
    public int payment_terms_in_days { get; set; }
    public string paid_on { get; set; }
    public string comments { get; set; }
}

public class RootObject
{
    public List<Invoice> invoices { get; set; }
}

RootObject list = JsonConvert.DeserializeObject<RootObject>(request);

RootObject对象包含发票列表

Json :(您的版本中缺少],靠近Json的末尾以关闭数组)

{
"invoices": [
    {
      "url": "https://api.freeagent.com/v2/invoices/000000",
      "contact": "https://api.freeagent.com/v2/contacts/000000",
      "dated_on": "2010-10-29",
      "due_on": "2010-11-28",
      "reference": "0000",
      "currency": "GBP",
      "exchange_rate": "1.0",
      "net_value": "960.0",
      "sales_tax_value": "568.0",
      "total_value": "528.0",
      "paid_value": "528.0",
      "due_value": "0.0",
      "status": "Paid",
      "omit_header": false,
      "payment_terms_in_days": 30,
      "paid_on": "2011-11-14"
    },
    {
      "url": "https://api.freeagent.com/v2/invoices/000000",
      "contact": "https://api.freeagent.com/v2/contacts/000000",
      "dated_on": "2010-11-19",
      "due_on": "2010-12-19",
      "reference": "0000",
      "currency": "GBP",
      "exchange_rate": "1.0",
      "net_value": "980.0",
      "sales_tax_value": "46.5",
      "total_value": "326.5",
      "paid_value": "650.0",
      "due_value": "76.5",
      "status": "Overdue",
      "comments": "Work",
      "omit_header": false,
      "payment_terms_in_days": 30
    }]
}

答案 1 :(得分:1)

  

有人可以告诉我如何获取字符串中显示的信息   出?

你可以这样做(动态):

   JavaScriptSerializer oSerializer = new JavaScriptSerializer();
   dynamic lst  = oSerializer.Deserialize<dynamic>(request );
   Console.WriteLine(lst["invoices"][0]["due_on"]); <--put here whatever you want.

例如,我读取了数组中第一项的值:

enter image description here