4

How can I covert this object string

products=[{"PRICE":39.95,"RECIPIENT":{"ZIPCODE":"11779"},"CODE":"F1-509"}] 

to QueryString in C# as a parameter in an URL?

I still have a problem because products - a JSON encoded string containing an array of product structures .Products is the parameter of the URL which the RECIPIENT object is inside RootObject.

var order = new RootObject()

        {
            Price = 39.95,
            Code = "F1-509",
            Recipient = new RECIPIENT
            {
                Zipcode = "11779"
            }
        };

I need to convert Products Parameter to query which pass by URL in C# with GetAsync(URL).

Fattaneh
  • 143
  • 1
  • 4
  • 13
  • If this is a small object you can split it and send multiple query strings or convert it to base64 and send as a single, but it's always preferred to post objects like this instead of get – Krishna Jul 13 '17 at 01:41
  • a quick google search `convert object to query string c#` displays some solutions [here](https://stackoverflow.com/q/6848296/465053) and [here](https://stackoverflow.com/q/9817591/465053) – RBT Jul 13 '17 at 01:46
  • @RBT does she need to serialize the object or just `UrlEncode` it? She had a similar post to this a little while ago that is now gone. She said this was working in Postman but not working in c#. Several questions were asked which were not responded to before that question was down voted and deleted. – Alexander Higgins Jul 13 '17 at 01:49
  • If you have a string then use Format : decimal price = 39.95M; string zipCode = "11779"; string code = "F1-509"; string query = string.Format("products=[{\"PRICE\":{0},\"RECIPIENT\":{\"ZIPCODE\":\"{1}\"},\"CODE\":\"{2}\"}]", price, zipCode, code); – jdweng Jul 13 '17 at 02:17
  • Fortunately I found the solution for converting array parameter to query parameter in URL. var queryString = JsonConvert.SerializeObject(object); var uri = WebUtility.UrlEncode(queryString); – Fattaneh Jul 15 '17 at 06:49

0 Answers0