如何阅读JSON响应?

时间:2014-06-16 09:32:07

标签: c# json httpresponse json-deserialization

我有一个API来验证手机并返回JSON数据。现在我想读取JSON 并以表格格式显示。

API调用,

protected void CallAPI()
{
    WebClient wsClient = new WebClient();
    string url = apiUrl;
    string response = string.Empty;
    string serialkey = txtserialNumber.Text;
    WebRequest req = WebRequest.Create(@"https://alt.computechsos.com/api/v1/xx");
    req.Method = "POST";
    req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("username:@password"));
    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
    WebHeaderCollection header = resp.Headers;
    string responseText = string.Empty;
    var encoding = ASCIIEncoding.ASCII;
    using (var reader = new System.IO.StreamReader(resp.GetResponseStream(), encoding))
    {
       responseText = reader.ReadToEnd();
    }

}

'responseText'现在包含以下JSON

Json返回了响应:

{"ios_info":{"serialNumber":"F2LLMBNJXXXQ","imeiNumber":"0138840041323XX","meid":"","iccID":"890141042764009604XX","firstUnbrickDate":"11\/27\/13","lastUnbrickDate":"11\/27\/13","unbricked":"true","unlocked":"false","productVersion":"7.1.1","initialActivationPolicyID":"23","initialActivationPolicyDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","appliedActivationPolicyID":"23","appliedActivationDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","nextTetherPolicyID":"23","nextTetherPolicyDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","macAddress":"ACFDEC6C9XXX","bluetoothMacAddress":"AC:FD:EC:6C:98:8B","partDescription":"IPHONE 5S SPACE GRAY 64GB-USA"},"product_info":{"serialNumber":"F2LLMBNJXXXQ","warrantyStatus":"Apple Limited Warranty","coverageEndDate":"11\/25\/14","coverageStartDate":"11\/26\/13","daysRemaining":"528","estimatedPurchaseDate":"11\/26\/13","purchaseCountry":"United States","registrationDate":"11\/26\/13","imageURL":"http:\/\/service.info.apple.com\/parts\/service_parts\/na.gif","explodedViewURL":"http:\/\/service.info.apple.com\/manuals-ssol.html","manualURL":"http:\/\/service.info.apple.com\/manuals-ssol.html","productDescription":"iPhone 5S","configDescription":"IPHONE 5S GRAY 64GB GSM","slaGroupDescription":"","contractCoverageEndDate":"11\/25\/15","contractCoverageStartDate":"11\/26\/13","contractType":"C1","laborCovered":"Y","limitedWarranty":"Y","partCovered":"Y","notes":"Covered by AppleCare+ - Incidents Available","acPlusFlag":"Y","consumerLawInfo":{"serviceType":"","popMandatory":"","allowedPartType":""}}}

如何读取此JSON并以表格格式显示数据?

2 个答案:

答案 0 :(得分:3)

使用Json-Net反序列化响应。要生成类,可以使用this generator来获取Json输入并输出相关的C#类。

    //....
    var responseObject = JsonConvert.DeserializeObject<RootObject>(responseText);
}

public class IosInfo
{
    public string serialNumber { get; set; }
    public string imeiNumber { get; set; }
    public string meid { get; set; }
    public string iccID { get; set; }
    public string firstUnbrickDate { get; set; }
    public string lastUnbrickDate { get; set; }
    public string unbricked { get; set; }
    public string unlocked { get; set; }
    public string productVersion { get; set; }
    public string initialActivationPolicyID { get; set; }
    public string initialActivationPolicyDetails { get; set; }
    public string appliedActivationPolicyID { get; set; }
    public string appliedActivationDetails { get; set; }
    public string nextTetherPolicyID { get; set; }
    public string nextTetherPolicyDetails { get; set; }
    public string macAddress { get; set; }
    public string bluetoothMacAddress { get; set; }
    public string partDescription { get; set; }
}

public class ConsumerLawInfo
{
    public string serviceType { get; set; }
    public string popMandatory { get; set; }
    public string allowedPartType { get; set; }
}

public class ProductInfo
{
    public string serialNumber { get; set; }
    public string warrantyStatus { get; set; }
    public string coverageEndDate { get; set; }
    public string coverageStartDate { get; set; }
    public string daysRemaining { get; set; }
    public string estimatedPurchaseDate { get; set; }
    public string purchaseCountry { get; set; }
    public string registrationDate { get; set; }
    public string imageURL { get; set; }
    public string explodedViewURL { get; set; }
    public string manualURL { get; set; }
    public string productDescription { get; set; }
    public string configDescription { get; set; }
    public string slaGroupDescription { get; set; }
    public string contractCoverageEndDate { get; set; }
    public string contractCoverageStartDate { get; set; }
    public string contractType { get; set; }
    public string laborCovered { get; set; }
    public string limitedWarranty { get; set; }
    public string partCovered { get; set; }
    public string notes { get; set; }
    public string acPlusFlag { get; set; }
    public ConsumerLawInfo consumerLawInfo { get; set; }
}

public class RootObject
{
    public IosInfo ios_info { get; set; }
    public ProductInfo product_info { get; set; }
}

答案 1 :(得分:0)

你需要一个包含你的json字符串所包含的所有属性的类,例如你有一个名为Jsondeserial的类

JsonDeserial obj = JsonConvert.DeserializeObject<JsonDeserial>(jsonstring);

这会将您的json字符串转换为该对象值