1

Current Code for SerializeObject :

dict1={"type":"check"}
dict2={"mac":"00:00:00:00:00:00"}

var json = JsonConvert.SerializeObject(dict1);
var jsonStr = JsonConvert.SerializeObject(dict2);

var arrayOfObjects = JsonConvert.SerializeObject(
new [] { JsonConvert.DeserializeObject(json ), JsonConvert.DeserializeObject(jsonStr) });

It convert the data in JSON like:

[
{
"type": "CHECK_CONNECTION"
},
{
"mac_address": "00:00:00:00:00"
}
]

I want final output like

{
"type":"check",
"mac_address":"00:00:00:00:00:00"
}
Akhilesh Mishra
  • 5,876
  • 3
  • 16
  • 32
kefi Kefi
  • 128
  • 10
  • Could [combine the dictionaries](https://stackoverflow.com/a/6695211/8395242) before serializing. – Andrew Jun 05 '20 at 19:12

3 Answers3

0
dict1={"type":"check"}
dict2={"mac":"00:00:00:00:00:00"}

var combinedObject = { "type": dict1.type, "mac": dict2.mac };
var jsonCustomObject = JsonConvert.SerializeObject(combinedObject);

Please see if this can help you. You were trying to serialize the array with 2 elements, and so it had reflected in the serialized content. If you want json representation of single object with 2 properties, then you will have create an object with 2 properties.

Dan-J
  • 19
  • 5
0
JObject jo = JObject.FromObject(item);
jo.Add("property_name", "property_value");
string json = jo.ToString();
kefi Kefi
  • 128
  • 10
0

You must to define a model to your datas. Like this:

>

//System.ComponentModel.DataAnnotations.Schema

public class Dict
{
    [JsonProperty(PropertyName = "type")]
    public string Type { get; set; } 

    [JsonProperty(PropertyName = "mac")]
    public string MacAdress { get; set; }
}

So, you can to serialise like this:

>

//Your code
dict1={"type":"check"}
dict2={"mac":"00:00:00:00:00:00"}

var type = JsonConvert.SerializeObject(dict1);
var mac = JsonConvert.SerializeObject(dict2);

var dict = new List<Dict>();

dict.Add(new Dict(){ Type = type, MacAdress = mac });

var jsonContent = JsonConvert.SerializeObject(dict, new JsonSerializerSettings()
{
    NullValueHandling = NullValueHandling.Ignore
});

Please tell me if this help you.