2017-03-02 50 views
1

我使用c#在asp.net mvc项目中使用域名的promo pricing api。我想获得像'dotgold'这样的特定产品密钥的代理商价格。在这个Api对象名称是59,58等动态的。我怎样才能做到这一点。下面是该API的JSON对象...当对象名称为动态时,如何获取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" 
    } 
} 

下面是desrialize的JSON对象

string redirect = @"https://test.httpapi.com/api/resellers/promo-details.json?auth-userid=xxxx&api-key=xxxxxxxxxxxx"; 
string html; 
var headers = new System.Net.WebHeaderCollection(); 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redirect); 
request.AutomaticDecompression = DecompressionMethods.GZip; 

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
Stream stream = response.GetResponseStream(); 
using (StreamReader reader = new StreamReader(stream)) 
{ 
    html = reader.ReadToEnd(); 
} 

var stuff = JObject.Parse(html2); 
+1

这真的很难帮你不知道你是如何在一般反序列化JSON或什么JSON的样子。请提供[mcve] - 并注意这不是ASP.NET特定的,因此控制台应用程序将使演示更加容易。 –

+0

我已经提供了json对象 –

+0

但是,您至今没有提供任何代码,您提供的JSON远非最小 - 我们不需要为每个值查看18个属性,只需足够做出具有代表性的样本。 –

回答

0

安装Newtonsoft.Json NuGet包,如果您还没有该代码。 然后使用它将json变成Dictionary<int, 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); 
      } 
     } 
    } 
} 
+0

它的工作..谢谢 –