3

I am implementing a utility method to convert queryString to JsonString.

My code is as follows:

        public static string GetJsonStringFromQueryString(string queryString)
        {
            var nvs = HttpUtility.ParseQueryString(queryString);
            var dict = nvs.AllKeys.ToDictionary(k => k, k => nvs[k]);
            return JsonConvert.SerializeObject(dict, new KeyValuePairConverter());
        }

when I test with the following code:

 var postString = "product[description]=GreatStuff" +
                    "&product[extra_info]=Extra"; 
 string json = JsonHelper<Product>.GetJsonStringFromQueryString(postString);

I got

{
    "product[description]":"GreatStuff",
    "product[extra_info]":"Extra",

      ...

   }

what I would like to get is

{
    "product":{
       "description": "GreatStuff",
       "extra_info" : "Extra",
       ...
    }
}

How can I achieve this without using System.Web.Script Assembly? (I am on Xamarin and have no access to that library)

Wei Ma
  • 3,125
  • 6
  • 44
  • 72
  • if you query string looks like this you have to write your own C# logic to parse them. var postString = "product[description]=GreatStuff" + "&product[extra_info]=Extra" + "&product[ledger_account_id]=42" + "&product[sales_price]=11.5" + "&product[sales_price_includes_tax]=0" + "&product[tax_code_id]=33"; – Prashant Jun 30 '15 at 19:22
  • see this http://stackoverflow.com/questions/15872658/standardized-way-to-serialize-json-to-query-string – Prashant Jun 30 '15 at 19:27

1 Answers1

0

You need to remove the product[key] (excepting the product property name or key...) part to get what you want...

That is, you should pre-process your query string before parsing it this way:

string queryString = "product[description]=GreatStuff" +
        "&product[extra_info]=Extra";
var queryStringCollection = HttpUtility.ParseQueryString(queryString);
var cleanQueryStringDictionary = queryStringCollection.AllKeys
                                    .ToDictionary
                                    (
                                        key => key.Replace("product[", string.Empty).Replace("]", string.Empty),
                                        key => queryStringCollection[key]
                                    );

var holder = new { product = cleanQueryStringDictionary };
string jsonText = JsonConvert.SerializeObject(holder);
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • This will work for single level of nested object. If I have some thing like product[vender][address][street_line1] = "8 hanson street". Is there any generic solution for this? – Wei Ma Jun 30 '15 at 19:52
  • @WeiMa AFAIK, no. At the end of the day, your query string is someway strange.... You're creating something like OData or who knows :( I mean, you need to customize your serialization from query string to JSON. Obviously you can develop some basic parser to even cover recursive conversion from query string to json............ – Matías Fidemraizer Jun 30 '15 at 19:57
  • 1
    @WeiMa I could drop here a self-made solution for this... but sadly I need to have dinner and sleep ;P BTW you can end up with something like a generic solution parsing each level of `[key1][keyN]` and doing something like I did in my answer but recursively, you know...... – Matías Fidemraizer Jun 30 '15 at 20:03
  • Yes, I got the idea. Evaluating if it is worth the effort to create a recursive solution. Thanks for your answer. – Wei Ma Jun 30 '15 at 20:15