0

I have an object that looks like this:

public class MyObj
{
    [JsonProperty("someProp")]
    public string someProp { get; set; }

    [JsonProperty("parameters")]
    [JsonExtensionData]
    public IDictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();
}

{
    "parameters" : [{"firstName": "John"}, {"lastName": "Doe"}]
}

How do I convert the above so that Parameters looks like this?

"firstName": "John", "lastName": "Doe"

What it looks like now when I post my data is

"parameters": "{"firstName": "John"}, {"lastName": "Doe"}]"
CBC_NS
  • 1,961
  • 4
  • 27
  • 47
  • 3
    you're missing something in that last JSON bit... it's not valid – Jeremy Holovacs Jul 11 '18 at 17:54
  • 1
    You may need to implement your own custom `JsonConverter` class to get that behavior. By default Newtonsoft just converts the dictionary itself to a JSON object, but it will be nested inside the object which had a dictionary property in C#. https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm – JamesFaix Jul 11 '18 at 18:02

2 Answers2

0

first of all your JSON should be like

{"parameters": [{"firstName": "John"}, {"lastName": "Doe"}]}

as you need {} signs to inform converter of the object and [] are for adding multiple values to one atribute. the "parameters": "{"firstName": "John"}, {"lastName": "Doe"}]" is not a valid JSON data.

After that it's just simple c# conversion. I would do it like this:

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, string>>(YOURJSONVARIABLE);
var name = dict['firstName'];

Of course for this to work you would need to add using System.Web to your c# file. Also... this option is compatibile with .net framework > 4.5 :)

Tomaszeek
  • 483
  • 1
  • 4
  • 11
-1

in js side,

var result=getYourData();
result.parameters= Object.keys(result.parameters).map(function(key){
    return {}[key]=result.parameters[key];
});

or

var result=getYourData();
result.parameters= Object.keys(result.parameters).map(function(key){
    var pram={};
    return pram[key]=result.parameters[key];
});

if c# code, you have 2 options(I don't recommend both):

  1. change dictionary<string, object> to List<KeyValuePair<string, object>> or KeyValuePair<string, object>[];
  2. use CustomConverter(too heavy solution, could be last choice).

if you take 1st option of c#, try this: Serialize List<KeyValuePair<string, string>> as JSON

The reason I don't recommend server-side change is: in my experience, one API could be reused in many parts, API should not specify many data format, but caller should massage response

Dongdong
  • 2,208
  • 19
  • 28
  • When I change dictionary to IEnumerable> my object is null when I post. – CBC_NS Jul 11 '18 at 18:28
  • I should have mentioned IEnumerable could be List<>,[] and so on. you don't have to use IEnumerable. if it is, use this: `new JavaScriptSerializer().Serialize(param);`. but if use `List>` or `KeyValuePair[]`, it will be converted automatically. – Dongdong Jul 11 '18 at 18:56