当对象名称是动态时,如何获取json对象的值?

时间:2017-03-02 07:59:31

标签: c# asp.net json

我正在使用c#在我的asp.net mvc项目中使用域名的促销定价api。我想得到像'dotgold'这样的特定产品密钥的经销商价格。在这个Api对象中,名称是动态的,如59,58等等。我怎样才能做到这一点。下面是那个api的json对象......

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient;

以下是对json对象进行去序列化的代码

{
  "59": {
    "customerprice": "528.00",
    "timestamp": "2017-02-10 13:59:16.711147+00",
    "actiontype": "addnewdomain",
    "resellerpricecurrencysymbol": "INR",
    "resellerpricetwo": "748.00",
    "isactive": "true",
    "starttime": "1486393372",
    "productkey": "dotjetzt",
    "creationdt": "1486735157",
    "promoid": "13333",
    "serviceprovidersellingcurrency": "INR",
    "istrickleallow": "true",
    "resellerpriceone": "489.50",
    "resellerid": "683272",
    "barrierprice": "680.0",
    "period": "1",
    "endtime": "1491004799",
    "resellerprice": "445.0"
  },
  "58": {
    "customerprice": "302.50",
    "timestamp": "2017-02-10 13:59:16.711147+00",
    "actiontype": "addnewdomain",
    "resellerpricecurrencysymbol": "INR",
    "resellerpricetwo": "451.00",
    "isactive": "true",
    "starttime": "1486393234",
    "productkey": "dotgold",
    "creationdt": "1486735157",
    "promoid": "13332",
    "serviceprovidersellingcurrency": "INR",
    "istrickleallow": "true",
    "resellerpriceone": "264.00",
    "resellerid": "683272",
    "barrierprice": "410.0",
    "period": "1",
    "endtime": "1491004799",
    "resellerprice": "240.0"
  }
}

1 个答案:

答案 0 :(得分:0)

如果您还没有安装Newtonsoft.Json nuget包。 然后使用它将json deserilize为Dictionary<int, Product>,其中product是你的dto类。您也可以反序列化为Dictinoary<int, dynamic>

这是一个例子

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Deserialize
{
    public class ProductDto
    {
        public decimal customerprice { get; set; }
        public DateTime timestamp { get; set; }
        public string actiontype { get; set; }
        public string resellerpricecurrencysymbol { get; set; }
        public string resellerpricetwo { get; set; }
        public string isactive { get; set; }
        public string starttime { get; set; }
        public string productkey { get; set; }
        public string creationdt { get; set; }
        public int promoid { get; set; }
        public string serviceprovidersellingcurrency { get; set; }
        public bool istrickleallow { get; set; }
        public decimal resellerpriceone { get; set; }
        public int resellerid { get; set; }
        public decimal barrierprice { get; set; }
        public string period { get; set; }
        public long endtime { get; set; }
        public decimal resellerprice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{" + Environment.NewLine +
                "  \"59\": {" + Environment.NewLine +
                "    \"customerprice\": \"528.00\"," + Environment.NewLine +
                "    \"timestamp\": \"2017-02-10 13:59:16.711147+00\"," + Environment.NewLine +
                "    \"actiontype\": \"addnewdomain\"," + Environment.NewLine +
                "    \"resellerpricecurrencysymbol\": \"INR\"," + Environment.NewLine +
                "    \"resellerpricetwo\": \"748.00\"," + Environment.NewLine +
                "    \"isactive\": \"true\"," + Environment.NewLine +
                "    \"starttime\": \"1486393372\"," + Environment.NewLine +
                "    \"productkey\": \"dotjetzt\"," + Environment.NewLine +
                "    \"creationdt\": \"1486735157\"," + Environment.NewLine +
                "    \"promoid\": \"13333\"," + Environment.NewLine +
                "    \"serviceprovidersellingcurrency\": \"INR\"," + Environment.NewLine +
                "    \"istrickleallow\": \"true\"," + Environment.NewLine +
                "    \"resellerpriceone\": \"489.50\"," + Environment.NewLine +
                "    \"resellerid\": \"683272\"," + Environment.NewLine +
                "    \"barrierprice\": \"680.0\"," + Environment.NewLine +
                "    \"period\": \"1\"," + Environment.NewLine +
                "    \"endtime\": \"1491004799\"," + Environment.NewLine +
                "    \"resellerprice\": \"445.0\" " + Environment.NewLine +
                "  }," + Environment.NewLine +
                "  \"58\": {" + Environment.NewLine +
                "    \"customerprice\": \"302.50\"," + Environment.NewLine +
                "    \"timestamp\": \"2017-02-10 13:59:16.711147+00\"," + Environment.NewLine +
                "    \"actiontype\": \"addnewdomain\"," + Environment.NewLine +
                "    \"resellerpricecurrencysymbol\": \"INR\"," + Environment.NewLine +
                "    \"resellerpricetwo\": \"451.00\"," + Environment.NewLine +
                "    \"isactive\": \"true\"," + Environment.NewLine +
                "    \"starttime\": \"1486393234\"," + Environment.NewLine +
                "    \"productkey\": \"dotgold\"," + Environment.NewLine +
                "    \"creationdt\": \"1486735157\"," + Environment.NewLine +
                "    \"promoid\": \"13332\"," + Environment.NewLine +
                "    \"serviceprovidersellingcurrency\": \"INR\"," + Environment.NewLine +
                "    \"istrickleallow\": \"true\"," + Environment.NewLine +
                "    \"resellerpriceone\": \"264.00\"," + Environment.NewLine +
                "    \"resellerid\": \"683272\"," + Environment.NewLine +
                "    \"barrierprice\": \"410.0\"," + Environment.NewLine +
                "    \"period\": \"1\"," + Environment.NewLine +
                "    \"endtime\": \"1491004799\"," + Environment.NewLine +
                "    \"resellerprice\": \"240.0\"" + Environment.NewLine +
                "  }" + Environment.NewLine +
                "}";

            var values = JsonConvert.DeserializeObject<Dictionary<int, ProductDto>>(json);

            foreach (var v in values)
            {
                var poductId = v.Key;
                var product = v.Value;
                Console.WriteLine(poductId + " " + product.resellerpriceone);
            }
        }
    }
}