0

I am returning a json string to a WebMethod in WebForms and I want to take the json string and parse it into custom Order objects.

I have a class:

public class Order
{
    public string Item { get; set; }
    public string Color { get; set; }
    public string Qty { get; set; }
    public string Size { get; set; }
}

And a WebMethod:

[WebMethod]
public static string SendOrder(string json)
{
    List<Order> orders = new List<Order>();

    return json;
}

I am passing this string:

{
    json: [
        {
            "Item":"Nike Polo #286772 - Women's Dri-FIT Micro Pique Short Sleeved Polo",
            "Size":"XL",
            "Color":"Light Blue",
            "Quantity":"3"
        },
        {
            "Item":"Port Authority Women's Jacket #L790 - Black",
            "Size":"Medium",
            "Color":"Black",
            "Quantity":"3"
        }
    ]
}

I want to loop through this string and creating new Orders.

What is the best way to do this?

3
  • 5
    I'd use JSON.Net. Have you tried anything yet? Commented May 11, 2015 at 17:53
  • Okay, I have added that reference and I am working on parsing the string into the objects. Commented May 11, 2015 at 18:04
  • This is how i would parse the json right: var orders = JsonConvert.DeserializeObject<List<Order>>(json); Commented May 11, 2015 at 18:08

2 Answers 2

2

That JSON is a little oddly formatted as it maps to the following classes (using http://json2csharp.com):

public class Json
{
    public string Item { get; set; }
    public string Size { get; set; }
    public string Color { get; set; }
    public string Quantity { get; set; }
}

public class RootObject
{
    public List<Json> json { get; set; }
}

I'm not sure why you have a top-level variable named json, but whatever.

At this point just use JSON.NET to deserialize into the structure.

JsonConvert.DeserializeObject<RootObject>(yourJsonString);

If you want to rename the object from Json to Order you'll need to use an attribute for that. I don't recall the name off the top of my head but it should be easy to find in the JSON.NET documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

I recently completed a Windows Phone app that retrieved info from a Web API-based server as Json strings. I ended up using the JsonConvert class to convert my lists of objects from Json strings to my custom objects. Here's an example of one of my client-side methods that receives and converts the Json strings:

public async void GetGames()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("base url");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("specific url extention (like api/Order)");

            if (response.IsSuccessStatusCode)
            {
                string s = await response.Content.ReadAsStringAsync();
                var deserializedResponse = JsonConvert.DeserializeObject<List<Order>>(s);
//rest of code


            }
        }
    }

Also, make sure that your web method is actually doing something. The example web method you posted creates creates a new list then just returns the parameter you passed in. Using Web API, you could return a list of all Order objects in your database via a method similar to the following:

public IQueryable<Order> GetOrders()
    {
        return db.Orders; //db is an instance of your DbContext class
    }

I hope this is helpful. Let me know if you have any questions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.