Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am attempting to query a REST API and I am able to successfully return the information to a string. I am trying to parse this string correctly into something that is more useful or visually appealing.

I am not sure how to tackle this. Here is the working code.

C#
namespace WFApi
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void dellSearchCmd_Click(object sender, EventArgs e)
        {
            try
            {
                queryFetch("https://sandbox.api.del.com/support/assetinfo/v4/getassetwarranty/" + serviceTagInput.Text + "?apikey=123456");
            }
            catch (WebException webex)
            {
                MessageBox.Show("Error Detected:", webex.Message);
            }

        }

        private void queryFetch(string uri)
        {       
            var wRequest = (HttpWebRequest)WebRequest.Create(uri);
            var wResponse = (HttpWebResponse)wRequest.GetResponse();
            var reader = new StreamReader(wResponse.GetResponseStream());
            string s = reader.ReadToEnd();
            textBox1.Text = s;      
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

 }


Now I can return String S into a textbox and the data looks like the following.

JavaScript
{"AssetWarrantyResponse":[{"AssetHeaderData":{"BUID":"11","ServiceTag":"JXYAHD","ShipDate":"2014-12-30T00:00:00","CountryLookupCode":"US","LocalChannel":"14","CustomerNumber":"1003","ItemClassCode":"5U004","IsDuplicate":false,"MachineDescription":"Latitude E7440","OrderNumber":"748942881","ParentServiceTag":null},"ProductHeaderData":{"SystemDescription":"Latitude E7440","ProductId":"latitude-e7440-ultrabook","ProductFamily":"Laptops","LOB":"Latitude","LOBFriendlyName":"Latitude"},"AssetEntitlementData":[{"StartDate":"2015-12-31T00:00:00","EndDate":"2017-12-30T23:59:59","ServiceLevelDescription":"Next Business Day Onsite","ServiceLevelCode":"ND","ServiceLevelGroup":5,"EntitlementType":"EXTENDED","ServiceProvider":"UNY","ItemNumber":"995-8084"},{"StartDate":"2015-12-31T00:00:00","EndDate":"2017-12-30T23:59:59","ServiceLevelDescription":"ProSupport","ServiceLevelCode":"TS","ServiceLevelGroup":8,"EntitlementType":"EXTENDED","ServiceProvider":"DELL","ItemNumber":"995-8774"},{"StartDate":"2014-12-30T00:00:00","EndDate":"2015-12-30T23:59:59","ServiceLevelDescription":"Next Business Day Onsite","ServiceLevelCode":"ND","ServiceLevelGroup":5,"EntitlementType":"INITIAL","ServiceProvider":"UNY","ItemNumber":"995-8054"},{"StartDate":"2014-12-30T00:00:00","EndDate":"2015-12-30T23:59:59","ServiceLevelDescription":"ProSupport","ServiceLevelCode":"TS","ServiceLevelGroup":8,"EntitlementType":"INITIAL","ServiceProvider":"DELL","ItemNumber":"995-8754"},{"StartDate":"2014-12-30T00:00:00","EndDate":"2023-01-02T23:59:59","ServiceLevelDescription":"Dell Digitial Delivery","ServiceLevelCode":"D","ServiceLevelGroup":11,"EntitlementType":"INITIAL","ServiceProvider":"DELL","ItemNumber":"422-0007"},{"StartDate":"2014-12-30T00:00:00","EndDate":"2023-01-02T23:59:59","ServiceLevelDescription":"Dell Digitial Delivery","ServiceLevelCode":"D","ServiceLevelGroup":11,"EntitlementType":"INITIAL","ServiceProvider":"DELL","ItemNumber":"421-9982"}]}],"InvalidFormatAssets":{"BadAssets":[]},"InvalidBILAssets":{"BadAssets":[]},"ExcessTags":{"BadAssets":[]},"AdditionalInformation":null}



Is there an easy way to place this into a datagrid or make this string easier to work with? For instance returning just the Shipdate or ServiceTag to the textbox?

Id personally like to make this dump into a datagrid so it can all be viewed.

Any recommendations or assistance would be greatly appreciated.
I've tried NewtonSoft and JavaSerializer to do so but have had no luck.
Posted
Comments
[no name] 15-Oct-15 1:00am    
What information do you need from Json data because it contains too much.

1 solution

You can create a class with same properties and then call
C#
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<t>() </t>
to deseralize json string to .NET object. Once you will have object list, you will be able to do anything.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900