DeserializeJSon question

I declare two classes:

[DataContract]
class WCProduct
{
    [DataMember(Name = "id")]
    public string ProductID { get; set; }
    [DataMember(Name = "sku")]
    public string Productsku { get; set; }
    [DataMember(Name = "title")]
    public string ProductName { get; set; }
    [DataMember(Name = "attributes")]
    public WCAttribute[] Attributes { get; set; }
}

[DataContract]
class WCAttribute
{
    [DataMember(Name = "id")]
    public string ProductID { get; set; }
    [DataMember(Name = "name")]
    public string NameID { get; set; }
    [DataMember(Name = "options")]
    public string options { get; set; }
}

But when I try to execute:

WCProduct jsonRoot = DeserializeJSon<WCProduct>(jsonString);
jsonResponse = jsonRoot.Attributes;

I got this error: "Error      4      Cannot implicitly convert type 'WCAttribute[]' to 'WCProduct[]'      C:\Users\rk688\AppData\Local\Temp\Vsta\SSIS_SC110\VstasRDzZrS92UOM8eUC6k9fZQ\VstaKyRL1rLgEkG4h0wJ__Kz6NQ\main.cs      429      32      SC_62c91189374d4fd6b180997972364e08"

Here is the function:

private WCProduct[] GetRestServiceResult1(string wUrl)
    {

        HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);

        if (Variables.UseStandardProxy)
        {
            httpWReq.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            httpWReq.PreAuthenticate = true;
        }
        HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
        WCProduct[] jsonResponse = null;

        try
        {
            //Test the connection
            if (httpWResp.StatusCode == HttpStatusCode.OK)
            {

                string jsonString = null;

                using (StreamReader reader = new StreamReader(httpWResp.GetResponseStream(), System.Text.Encoding.UTF8))
                {
                    jsonString = reader.ReadToEnd();
                    reader.Close();
                }

                WCProduct jsonRoot = DeserializeJSon<WCProduct>(jsonString);
                jsonResponse = jsonRoot.Attributes;
            }
            //Output connection error message
            else
            {
                FailComponent(httpWResp.StatusCode.ToString());

            }
        }
        //Output JSON parsing error
        catch (Exception e)
        {
            FailComponent(e.ToString());
        }
        return jsonResponse;

    }

Can you please tell me WHERE is my mistake?
BurzhuinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
ambienceCommented:
Shouldnt

WCProduct[] jsonResponse = null;

be

WCAttribute[] jsonResponse = null;
0
 
BurzhuinAuthor Commented:
Now this line return the same error:

return jsonResponse;

Here is the error:

Error      4      Cannot implicitly convert type 'WCAttribute[]' to 'WCProduct[]'      C:\Users\rk688\AppData\Local\Temp\Vsta\SSIS_SC110\VstasRDzZrS92UOM8eUC6k9fZQ\VstaKyRL1rLgEkG4h0wJ__Kz6NQ\main.cs      443      16      SC_62c91189374d4fd6b180997972364e08
0
 
ambienceCommented:
Return jsonRoot from the method (instead of jsonResponse) and adjust the scope etc accordingly if it raises a few other compiler errors.
0
 
BurzhuinAuthor Commented:
One thing I am trying to figure out:

It works perfectly fine in another situation:

WCOrders jsonRoot = DeserializeJSon<WCOrders>(jsonString);
jsonResponse = jsonRoot.Orders;

Here is Class declaration:

[DataContract]
class WCOrders
{
    [DataMember(Name = "orders")]
    public WCOrder[] Orders { get; set; }
}
//Class to hold our work group metrics
[DataContract]
class WCOrder
{
    [DataMember(Name = "id")]
    public int OrderId { get; set; }
    ...
    public WCOrderLine[] OrderLines { get; set; }
}

WCOrder is a child of WCOrders class.

Please refer to my initial posting where WCAttribute is a child of WCProduct. But in one instance it is working fine and in another it is returning the error. I am trying to figure out WHY. Can somebody explain WHAT am I doing wrong?
0
 
sarabandeCommented:
WCOrder is a child of WCOrders class.

WCOrders "has" a collection of orders of type WCOrder and because of that you can get the array of WCOrder entries from it and assign it to another array of WCOrders.

jsonResponse = jsonRoot.Orders;

Open in new window


you try to assign an array of WCAttribute to an array of WCProduct and the compiler is correct to complain about.

Now this line return the same error:
 return jsonResponse;

GetRestServiceResult1 is defined to return an array of WCProduct. if you change 'jsonResponse' from WCProduct[] to WCAttribute[], the compiler is again right to throw an error if you try to return an array of WCAttribute instead of  WCProduct[].

it should be possible to find out whether the GetRestServiceResult1 is supposed to return a list of products (A) or a list of attributes (B) or only one product containing a list of attributes (C).

(A)  is not very likely since the DeserializeJSon seems to return only one product.

(B) is possible but you have to change the return type of your function from WCProduct[] to WCAttribute[]

(C) is most likely but you have to change the return type of your function from WCProduct[] to WCProduct and return jsonRoot as suggested by ambience.

Sara
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.