Json Object to a Multidimensional C# Array? - c#

Is there a way to convert a Json Object to a Multidimensional C# Array? I know it might be impractical but I can't be bothered to write classes and then deserialize the strings into them.
List<string> ohyeah = (List<string>)JsonConvert.DeserializeObject(g.CommToken);
That returns an Invalid Cast exception!
Example:
{"method":"getCommunicationToken","header":{"uuid":"9B39AAB0-49A6-AC7A-BA74-DE9DA66C62B7","clientRevision":"20100323.02","session":"c0d3e8b5d661f74c68ad72af17aeb5a1","client":"gslite"},"parameters":{"secretKey":"d9b687fa10c927f102cde9c085f9377f"}}
I need to get something like that :
j["method"]; //This will equal to getCommunicationToken
j["header"]["uuid"]; //This will equal to 9B39AAB0-49A6-AC7A-BA74-DE9DA66C62B7
I literally need to parse the json object into an array.

The Parse and SelectToken methods of the JObject class do exactly what you want/need.
The Library can be found here: http://json.codeplex.com/releases/view/37810
JObject o = JObject.Parse(#"
{
""method"":""getCommunicationToken"",
""header"":
{
""uuid"":""9B39AAB0-49A6-AC7A-BA74DE9DA66C62B7"",
""clientRevision"":""20100323.02"",
""session"":""c0d3e8b5d661f74c68ad72af17aeb5a1"",
""client"":""gslite""
},
""parameters"":
{
""secretKey"":""d9b687fa10c927f102cde9c085f9377f""
}
}");
string method = (string)o.SelectToken("method");
// contains now 'getCommunicationToken'
string uuid = (string)o.SelectToken("header.uuid");
// contains now '9B39AAB0-49A6-AC7A-BA74DE9DA66C62B7'
By the way: This is not a multidimensional array:
j["header"]["uuid"];
You would have those indexers for example in a dictionary with dictionaries as values, like:
Dictionary<string, Dictionary<string, string>> j;
But here you would just have a "depth" of two indexes. If you want a datastructure with indexers in this "jagged array style", you would write a class like:
class JaggedDictionary{
private Dictionary<string, string> leafs =
new Dictionary<string, string>();
private Dictionary<string, JaggedDictionary> nodes =
new Dictionary<string, JaggedDictionary>();
public object this[string str]
{
get
{
return nodes.Contains(str) ? nodes[str] :
leafs.Contains(str) ? leafs[str] : null;
}
set
{
// if value is an instance of JaggedDictionary put it in 'nodes',
// if it is a string put it in 'leafs'...
}
}
}

Related

JSON Deserialization for dictionary with arrays

I have a Python code that sends this JSON through MQTT.
message = {
"name":"Alex",
"date": 2021,
"activity":["act1","act2","act3"],
}
Then I receive and Deserialize it in a C# script
public void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
var Message = System.Text.Encoding.Default.GetString(e.Message);
Dictionary<string, string> MessageDICT = JsonConvert.DeserializeObject<Dictionary<string, string>>(Message);
}
The keys "name" and "date" has no problem being deserialized into the dictionary. However the error comes with "activity" due it being an array. Where it states "Unexpected character encountered while parsing value:[". I have seen methods where they deserialize it separately (where the array is sent in a different message), however this is not what I want. Is there a way I can deserialize the message as a whole?
Thanks.
You have your dictionary declared as <string, string>, but "activity" is an array. So this is why it does not work.
If you want to keep using a dictionary, then you can instead declare it as <string, object> and then it will work for any type of data. But you will need to then check what Type it is before you later try to use it.
Dictionary<string, object> MessageDICT = JsonConvert.DeserializeObject<Dictionary<string, object>>(message);
You can then access it like this:
object first = ((JArray)MessageDICT["activity"])[0];
But if possible, you should try to use a fixed class for the data and deserialize that.
Otherwise, maybe a dynamic is better.
Hi why not use a class to read the json
public class Message
{
public string name {get;set;}
public string date {get;set;} //actually, this looks like an int the json, however...
public string[] activity {get;set;}
}
usage
var res = JsonConvert.DeserializeObject<Message>(message);

Serialize the List<string> value within a Dictionary<string, List<string>> as simple array

Using the Mongo .Net Driver 2.7.2, I am attempting to customize how a Dictionary property is getting serialized. Specifically, I would like the Dictionary's value, a List<string> to serialize as a simple array.
Current document structure generated by serializer
This is how the property is currently serializing. As you can see, somePropertyis being serialized as an object, with _t and _v storing the .NET type and value.
viewedBy: Object
someProperty: Object
_t: "System.Collections.Generic.List`1[System.String]"
_v: Array
I understand the type information is stored for deserialization back into the c# POCO, but for my case I don't need this metadata, as my type is just a string array.
Desired document structure
I would like the property values to serialize as simple arrays of strings,as below
viewedBy: Object
someProperty: Array
Here is the class I am attempting to customize the serialization strategy for.
public class MyDocument
{
public Dictionary<string, List<string>> ViewedBy { get; set; }
}
Current Mapping
Here is the mapping information I have provided for my class.
BsonClassMap.RegisterClassMap<MyDocument>(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.ViewedBy)
.SetElementName("viewedBy")
.SetSerializer(new
DictionaryInterfaceImplementerSerializer<Dictionary<string,
List<string>>>(DictionaryRepresentation.Document));
// How do I specify the dictionary value should serialize as simple array?
});
Question
How can I instruct the serializer to serialize the dictionary's value, of .NET type List<string> to serialize into a simple string array?
Preference is for a fluent mapping solution, rather than using Attributes.
Include System.Linq
use this the way you want.
Dictionary<string, List<string>> m = new Dictionary<string, List<string>>();
m.Add("1", new List<string> { "Sumit", "Raj" });
m.Add("2", new List<string> { "Rahul", "Raj" });
/*Array of string. Currently 4 values*/
var res = m.SelectMany(x => x.Value).ToArray();
/*Array of List<string>. Currently 2 List With 2 Values*/
var res1 = m.Select(x => x.Value).ToArray();

How to deserialise json string in C# using JavascriptSerializer

I am trying to get value of label from following string.
I have a string in this format.
var string = "[{\"key\":\"182\",\"label\":\"testinstitution\"}]"
dynamic dict = new JavaScriptSerializer().Deserialize<dynamic>(string);
string inst = dict["label"]; //This is not working
I am getting dict in form of key value pair object but I am not able to get value of label. I cannot use JSON.NET.
To retrieve value of label from your string use string inst = dict[0]["label"];.
Explanation
The reason why you need additional [0] is because deserialization returns array of key value pairs. First object from that array will go to index [0], second object from array to index [1] an so on. Your string has array of only one object. Here is an example of when you have two objects, where second object has another object inside of it, in which case you would have to write dict[1]["foo"]["two"] to get to desired value:
var myString = #"
[
{
'one': '1'
},
{
'foo':
{
'two': '2'
}
}
]";
dynamic dict = new JavaScriptSerializer().Deserialize<dynamic>(myString);
string inst = dict[1]["foo"]["two"];
Additional FYI
If you know structure of your data consider using strong types (as suggested in one of the comments). Here is example of how you would do it:
public class Data
{
public string key { get; set; }
public string label { get; set; }
}
class Program
{
static void Main(string[] args)
{
var myString = #"
[{
'key': 182,
'label': 'testinstitution'
}]";
List<Data> dict = new JavaScriptSerializer().Deserialize<List<Data>>(myString);
foreach (var d in dict)
Console.WriteLine(d.key + " " + d.label);
Console.ReadKey();
}
}
Note that properties key and value in your Data object much match names of those in your array of objects exactly.

Convert JSON array of objects with specified properties to Dictionary

I need to convert to dictionary JSON array of objects with key "id" and value "score" properties like the following
[{"score":0.6990418,"id":"4833909335"},
{"score":0.8079009,"id":"4833874639"},
{"score":0.8079009,"id":"4834247506"}].
Dictionary keys should have values of "id" property, and dictionary values shoud be copied from "score".
Note that it is not a deserialization, but transforming/mapping when some information not copied to target object(e.g. names of properties and potentially other properties)
I found similar question How to deserialize the json array of objects to dictionary , that was incorrectly closed as duplicate of different question about deserializing different JSON object with multiple properties.
I've tried to reopen it, but without success.
I've created extension methods to allow extract pair of properties from elements of JSON array
public static Dictionary< string, string> JsonArrayToDictionary( string strJson, string keyName,string valueName)
{
var array = JArray.Parse(strJson) ;
var dictionary = JsonArrayToDictionary(array, keyName, valueName);
return dictionary;
}
public static Dictionary<string , string > JsonArrayToDictionary(this JArray array, string keyName, string valueName)
{
if (array != null)
{
var dict = array.ToDictionary(x => x[keyName].ToString(), x => x[valueName].ToString());
return dict;
}
return null;
}
[TestClass()]
public class JsonHelperExtensionsTests
{
[ TestMethod()]
public void JsonArrayToDictionaryTest()
{
var jsonArray= #"[{""score "":0.6990418,"" id"": ""ID1"" },{""score "":0.8079009,"" id"": ""ID2"" }]";
var dict= JsonHelperExtensions.JsonArrayToDictionary(jsonArray, "id" , "score");
dict.Count.Should().Be(2);
dict[ "ID1"].Should().Be("0.6990418" );
}
}
Current solution for current time:
var jsArray = JObject.Parse(response);
var arrayDictionary = JsonConvert.DeserializeObject<Dictionary<string, dynamic>[]>(jsArray.ToString());

Is there any php like array type in .NET

I am good at working with PHP arrays because it goes multidimensional like we can use the mixed array types in PHP like
Array["first_array"][0]=>1
[1]=>"Rehmat"
["second_array"][0]=>1
[1]=>"asd"
Can we have these type of arrays ? in .net if yes please guide me
So it looks like you have an outer array that maps a string to an inner array, and the inner array maps an integer key to a value, where that value could be any type. That can be represented like so:
Dictionary<string, object[]> dictionary = new Dictionary<string, object[]>()
{
{"first_array", new object[]{1, "Rehmat"}},
{"second_array", new object[]{1, "asd"}},
}
This is of course a direct conversion; this solution is not particularly idiomatic in C#.
You should most likely have the value of the dictionary instead but a custom object:
public class Foo //TODO rename
{
public int ID {get;set;}
public string Name {get;set;}
}
Then have a dictionary mapping strings to those objects:
Dictionary<string, Foo> dictionary;

Categories