0

In the following code, I have a dictionary "nissi_params_fields" which I have populated with parameters:

Dictionary<string, string> nissi_params_fields = new Dictionary<string, string>();
string[] separator = { "," };
string[] dfields = form_fields.Split(separator, StringSplitOptions.RemoveEmptyEntries);
string[] ffields = db_fields.Split(separator, StringSplitOptions.RemoveEmptyEntries);

foreach (var field in ffields)
{
    NissiMain nm = new NissiMain();
    object field_object = nm.nissi_get_object_by_name(field);
    string fieldvalue = nm.nissi_get_object_value_by_name(field_object);
    nissi_params_fields[field] = fieldvalue;
    this.nissiSetStorageItem(save_page, field, fieldvalue);
}
nissi_params_fields["company_id"] = this.nissiGetStorageItem("nissi_base", "ni_companyID");
string nissi_params_id = "";

if (save_type == "edit")
{
    nissi_params_fields["id"] = this.nissiGetStorageItem(save_page, "id");
    nissi_params_id = this.nissiGetStorageItem(save_page, "id");
}

I now want to create an anonymous type that contains the above "nissi_params_fields" dictionary as a single field "fields", so I first try to convert "nissi_params_fields" to an object "nissi_params_fields_object" that I can use in the Newtonsoft JObject "nissi_params_object":

object nissi_params_fields_object = nissi_params_fields.ToArray();

The challenge is how to convert the dictionary to an object ...how do I do this?

I now want to include the converted object "nissi_params_fields_object" in an anonymous type and then serialize the entire thing to JSON using the Newtonsoft JObject:

JObject nissi_params_object = JObject.FromObject(new
{
    apikey = this.nissiGetStorageItem("nissi_base", "ni_apiKey"),
    company_id = this.nissiGetStorageItem("nissi_base", "ni_companyID"),
    id = nissi_params_id,
    fields = nissi_params_fields_object,
});
bolaji
  • 51
  • 5
  • why do you want to convert the dictionary? – Raja Nadar Apr 09 '14 at 04:27
  • Thanks pravprab! I simply want to create url string (with JSON parameter) in the format /url.php?nissi_request=MyRequest&nissi_params={"apikey":"xxyyzz","company_id":"1",fields={"lastname":"hello","firstname":"world"}} – bolaji Apr 09 '14 at 08:18

1 Answers1

0

If you just want to JSON serialize the object you can do:

string jsonString = JsonConvert.SerializeObject(nissi_params_object);

and then append jsonString to the URL.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41