How could you convert a string of JSON to a C# NameValueCollection simply, preferably without using a 3rd party parser?
I'm not sure why everyone is still recommending JSON.NET for deserialization of JSON. I wrote a blog post on how to deserialize JSON to C#.
In short, it's like this:
using System.Web.Script.Serialization;
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, string>>(jsonText);
NameValueCollection nvc = null;
if (dict != null) {
nvc = new NameValueCollection(dict.Count);
foreach (var k in dict) {
nvc.Add(k.Key, k.Value);
}
}
}
var json = jss.Serialize(dict);
Console.WriteLine(json);
Be sure to add a reference to System.Web.Extensions.dll.
Note:
I usually deserialize to dynamic, so I'm assuming that NameValueCollection would work. However, I haven't verified if it actually does.
EDIT
Pure .net solution without third party development have look : JavaScriptSerializer – Dictionary to JSON Serialization and Deserialization
make use of Json.NET
string jsonstring = #"{""keyabc"":""valueabc"",""keyxyz"":""valuexyz""}";
Dictionary<string, string> values =
JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonstring);
Check #jon answer suggest same : .Net Linq to JSON with Newtonsoft JSON library
If your JSON contains nested objects whithin it the solution below will handle them properly (based on JSON.NET, but you can adapt to the JSON parser of your choice).
This usage example:
var json = "{\"status\":\"paid\",\"date\":\"2019-10-09T17:30:51.479Z\",\"customer\":{\"id\":123456789,\"country\":\"br\",\"name\":\"Thomas Vilhena\",\"phone_numbers\":[\"+5511987654321\"],\"documents\":[{\"id\":\"doc_id\"}]}}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
var nvc = new NameValueCollection(dict.Count);
AddKeyValuePairs(nvc, dict);
Console.WriteLine(nvc["status"]);
Console.WriteLine(nvc["date"]);
Console.WriteLine(nvc["customer[phone_numbers][0]"]);
Console.WriteLine(nvc["customer[id]"]);
Console.WriteLine(nvc["customer[documents][0][id]"]);
Produces the following output:
paid
09.10.2019 17:30
+5511987654321
123456789
doc_id
And here's the implementation:
private static void AddKeyValuePairs(
NameValueCollection nvc,
Dictionary<string, object> dict,
string prefix = null)
{
foreach (var k in dict)
{
var key = prefix == null ? k.Key : prefix + "[" + k.Key + "]";
if (k.Value != null)
AddKeyValuePair(nvc, key, k.Value);
}
}
private static void AddKeyValuePair(
NameValueCollection nvc,
string key,
object value)
{
if (value is string || value.GetType().IsPrimitive)
{
nvc.Add(key, value.ToString());
}
else if (value is DateTime)
{
nvc.Add(key, ((DateTime)value).ToString("g"));
}
else
{
AddNonPrimitiveValue(nvc, key, value);
}
}
private static void AddNonPrimitiveValue(
NameValueCollection nvc,
string key,
object value)
{
var a = value as JArray;
if (a != null)
{
for (int i = 0; i < a.Count; i++)
AddKeyValuePair(nvc, key + "[" + i + "]", a[i]);
}
else
{
var v = value as JValue;
if (v != null)
{
AddKeyValuePair(nvc, key, v.Value);
}
else
{
var j = value as JObject;
if (j != null)
AddKeyValuePairs(nvc, j.ToObject<Dictionary<string, object>>(), key);
}
}
}
Related
I'm able to get a dynamic Json object using
dynamic obj = JsonConvert.DeserializeObject(json);
It seems to be a nested object structure
I need to graph every variable in the json file, but the json file structure changes often
Is there a way to parse through this structure using nested foreach() statements?
If not, can can I parse it by accessing each element via a string like a Dictionary?
for example something like:
if(obj["Item1"]["Parameter3"]["Value2"]` != NULL)
int number = obj["Item1"]["Parameter3"]["Value2"]`
Thanks,
Yes there is an API for querying dynamically.
See the documentation here: https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm
Code looks something like this:
JObject rss = JObject.Parse(json);
var postTitles =
from p in rss["channel"]["item"]
select (string)p["title"];
Finally figured this api out
Some JToken entries have a list of values, others have a name and value. You have to sort which one is which prior to parsing it.
This will create a Dictionary with every entry in the Json file
void SomeFunction()
{
Dictionary<string, decimal> json_data = new Dictionary<string, decimal>();
dynamic json_obj = JsonConvert.DeserializeObject(json);
Linearize(ref json_data, json_obj);
}
void Linearize(ref Dictionary<string, decimal> input_dict, JToken json_data, string key = "")
{
int i;
if (json_data != null)
{
if (json_data.HasValues)
{
i = 0;
foreach (dynamic entry in json_data)
{
//Add a Name field if it exists
Type typeOfDynamic = entry.GetType();
if (typeOfDynamic.GetProperties().Where(p => p.Name.Equals("Name")).Any())
key += entry.Name + ".";
//If JToken is an Array
if (((JToken)entry).HasValues)
{
Linearize(ref input_dict, entry, key + "[" + i++ + "]" + ".");
}
//If JToken is a data type
else if (entry.Type == JTokenType.String || entry.Type == JTokenType.Float || entry.Type == JTokenType.Integer)
{
decimal output;
if (decimal.TryParse(entry.ToString(), out output))
input_dict.Add(key + "[" + i++ + "]", output);
}
}
}
}
}
The best I found was that available solutions at stack do not have answer for nested json object, they only address liner json values. But I have data to send is like
{ ob: { a: "78", b: { a: "gffg", h: {m:67, j:"fff"} } } }
If I want to do it in php i would just do
$json = $_POST['ob'];
$obj = json_decode($json);
But In c# I can'not do that. So I would appreciate any built-in method if available and I would love to be helped to fix my following code
I want to make a nested dictionary (I would prefer JOBject though). For ease of showing output I have serialized the result,
What result I have achieved yet from following code is
{"a":"78","ob":{},"ob.b":{"a":"gffg"},"ob.b.h":{"m":"67","j":"fff"}} but desired result is like sent data { "ob": { "a": "78", "b": { "a": "gffg", "h": {m:67, "j":"fff"} } } } Code is
string getJsonStringFromQueryString()
{
Dictionary<string, object> dic = new Dictionary<string, object>();
var nvc = Request.QueryString;
foreach (string key in nvc.Keys)
{
string[] values = nvc.GetValues(key);
string tempKey = key;
tempKey = tempKey.Replace("[", ".").Replace("]", "");
if (values.Length == 1)
dic.Add(tempKey, values[0]);
else
dic.Add(tempKey, values);
}
//It is giving me
{[ob.a, 78]}
{[ob.b.a, gffg]}
{[ob.b.h.m, 67]}
{[ob.b.h.j, fff]}
var result = makeNestedObject(dic);
var json = new JavaScriptSerializer().Serialize(result);
return json;
}
I am trying to add the leaf keys and their values as it is and all other keys as dictionary
Dictionary<string, object> makeNestedObject(Dictionary<string, object> qsDictionar)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (string key in qsDictionar.Keys)
{
string temp = "";
if (key.Contains("."))
{
string[] ar = key.Split('.');
if (ar.Length > 2)
{
for (int i = 0; i < ar.Length - 1; i++)
{
temp = ar[0];
for (int j = 1; j <= i; j++)
{
temp += "." + ar[j];
}
//above is getting the previous key i want to use as dictionary, leaving the leaf key.
try
{
Dictionary<string, object> forTry = (Dictionary<string, object>)result[temp];
}
catch
{
result.Add(temp, new Dictionary<string, object>());
}
}
((Dictionary<string, object>)result[temp]).Add(ar[ar.Length - 1], qsDictionar[key]);
}
else
result.Add(ar[1], qsDictionar[key]);
}
}
return result;
}
Following method gives you complete solution for any json object.
string getJsonStringFromQueryString()
{
Dictionary<string, object> dic = new Dictionary<string, object>();
var nvc = Request.QueryString;
foreach (string key in nvc.Keys)
{
string[] values = nvc.GetValues(key);
string tempKey = key;
tempKey = tempKey.Replace("[", ".").Replace("]", "");
if (values.Length == 1)
dic.Add(tempKey, values[0]);
else
dic.Add(tempKey, values);
}
string vald = Request.QueryString["ob"];
var result = makeNestedObject(dic);
var json = new JavaScriptSerializer().Serialize(result);
return json;
}
Dictionary<string, object> makeNestedObject(Dictionary<string, object> qsDictionar)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (string key in qsDictionar.Keys)
{
if (key.Contains("."))
{
List<string> keysList = key.Split('.').ToList();
Dictionary<string, object> lastAddedDictionary = result;
while (keysList.Count > 1)
{
if (!lastAddedDictionary.ContainsKey(keysList[0]))
{
Dictionary<string, object> temp = new Dictionary<string, object>();
lastAddedDictionary[keysList[0]] = temp;
lastAddedDictionary = temp;
}
else
lastAddedDictionary = (Dictionary<string, object>)lastAddedDictionary[keysList[0]];
keysList.RemoveAt(0);
}
lastAddedDictionary[keysList[0]] = qsDictionar[key];
}
else
{
result.Add(key, qsDictionar[key]);
}
}
return result;
}
I have a JObject like this:
{
"#STARTDATE": "'2016-02-17 00:00:00.000'",
"#ENDDATE": "'2016-02-18 23:59:00.000'"
}
I want to get #STARTDATE and #ENDDATE value from JObject.
This is a sample code that I've tried to do the task:
JObject json = JObject.Parse("{\"#STARTDATE\": \"'2016-02-17 00:00:00.000'\",\"#ENDDATE\": \"'2016-02-18 23:59:00.000'\"}");
var key = "#STARTDATE";
var value = GetJArrayValue(json, key);
private string GetJArrayValue(JObject yourJArray, JToken key)
{
string value = "";
foreach (JToken item in yourJArray.Children())
{
var itemProperties = item.Children<JProperty>();
//If the property name is equal to key, we get the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == key.ToString());
value = myElement.Value.ToString(); //It run into an exception here because myElement is null
break;
}
return value;
}
Note: The code above cannot get the value by key from JObject.
Could you help me to find a way to get the value by key from JObject?
This should help -
var json = "{'#STARTDATE': '2016-02-17 00:00:00.000', '#ENDDATE': '2016-02-18 23:59:00.000' }";
var fdate = JObject.Parse(json)["#STARTDATE"];
You can also get the value of an item in the jObject like this:
JToken value;
if (json.TryGetValue(key, out value))
{
DoSomething(value);
}
Try this:
private string GetJArrayValue(JObject yourJArray, string key)
{
foreach (KeyValuePair<string, JToken> keyValuePair in yourJArray)
{
if (key == keyValuePair.Key)
{
return keyValuePair.Value.ToString();
}
}
}
I have below json in string as parameter to a WebMethod.
How can I deserialize in such a way that value comes in Key value pair.
Json String Parameter:
["Ref No,0","Date,0","Amt,0","Sender Name,0","Sender Add,0","Beneficiary Name,0","Beneficiary Add,0","Phone,0","Secret Code,0","Secret Ans,0","Preferred Id,0"]
WebMethod:
[System.Web.Services.WebMethod]
public static string SaveMappings(string mappingData)
{
//string str = "{\"Arg1\":\"Arg1Value\",\"Arg2\":\"Arg2Value\"}";
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//object obj;
//var data = serializer.Deserialize(mappingData,);
var data = mappingData.ToArray();
if (data != null)
{
}
var d2 = mappingData.Split(',');
if (d2!=null)
{
}
return mappingData;
}
If you need to work with JSON data then use Newtonsoft.JSON library.
Convert the object to an array of strings and then split every line.
With this approach you can be sure that the given string is actually an JSON array and it is correct.
var str = "[\"Ref No,0\",\"Date,0\",\"Amt,0\",\"Sender Name,0\",\"Sender Add,0\",\"Beneficiary Name,0\",\"Beneficiary Add,0\",\"Phone,0\",\"Secret Code,0\",\"Secret Ans,0\",\"Preferred Id,0\"]";
string[] objs = JsonConvert.DeserializeObject<string[]>(str);
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (var obj in objs)
{
var keyValue = obj.Split(',');
dic.Add(keyValue[0], keyValue[1]);
}
foreach (var record in dic)
{
Console.WriteLine("{0} => {1}", record.Key, record.Value);
}
Or this one using LINQ. It looks better and it can be written faster. However, it is less optimal (two calls of Split instead of one).
public Dictionary<string, string> FromJsonArray(string jsonArray)
{
return JsonConvert.DeserializeObject<string[]>(jsonArray)
.ToDictionary(obj => obj.Split(',')[0], obj => obj.Split(',')[1]);
}
// ...
var str = "[\"Ref No,0\",\"Date,0\",\"Amt,0\",\"Sender Name,0\",\"Sender Add,0\",\"Beneficiary Name,0\",\"Beneficiary Add,0\",\"Phone,0\",\"Secret Code,0\",\"Secret Ans,0\",\"Preferred Id,0\"]";
foreach (var record in FromJsonArray(str))
{
Console.WriteLine("{0} => {1}", record.Key, record.Value);
}
why don't you just change every ',' in the string array to ':' then pass it to the method, from what you wrote in the question, this should work
I'd like to convert the JSON string
"{ \"birthday\": \"1988-03-18\", \"address\": { \"state\": 24, \"city\": 8341, \"country\": 1 } }"
to
"{ \"address\": { \"city\": 8341, \"country\": 1, \"state\": 24 }, \"birthday\": \"1988-03-18\" }"
NOTE: I'm not using the sorted version for communication (because the key order doesn't really matter), I need a sorted version to perform local tests (by comparing JSON strings).
EDIT: I4V pointed a solution that uses Json.Net, I would rather use a solution that doesn't need to include any 3rd party library (actually I'm using the built in System.Json in my application)
I posted a gist with the solution provided by I4V + some testing here. Thank you all.
I will use Json.Net for this
string json = #"{ ""birthday"": ""1988-03-18"", ""address"": { ""state"": 24, ""city"": 8341, ""country"": 1 } }";
var jObj = (JObject)JsonConvert.DeserializeObject(json);
Sort(jObj);
string newJson = jObj.ToString();
void Sort(JObject jObj)
{
var props = jObj.Properties().ToList();
foreach (var prop in props)
{
prop.Remove();
}
foreach (var prop in props.OrderBy(p=>p.Name))
{
jObj.Add(prop);
if(prop.Value is JObject)
Sort((JObject)prop.Value);
}
}
EDIT
A try with System.Json but I am not sure about OrderByDescending ( or OrderBy).
var jObj = (System.Json.JsonObject)System.Json.JsonObject.Parse(json);
Sort2(jObj);
var newJson = jObj.ToString();
void Sort2(System.Json.JsonObject jObj)
{
var props = jObj.ToList();
foreach (var prop in props)
{
jObj.Remove(prop.Key);
}
foreach (var prop in props.OrderByDescending(p => p.Key))
{
jObj.Add(prop);
if (prop.Value is System.Json.JsonObject)
Sort2((System.Json.JsonObject)prop.Value);
}
}
I know this may be a little late but, in case of you need to sort the internal arrays of data too (I just needed it):
static void Sort(JObject jObj)
{
var props = jObj.Properties().ToList();
foreach (var prop in props)
{
prop.Remove();
}
foreach (var prop in props.OrderBy(p => p.Name))
{
jObj.Add(prop);
if (prop.Value is JObject)
Sort((JObject)prop.Value);
if (prop.Value is JArray)
{
Int32 iCount = prop.Value.Count();
for (Int32 iIterator = 0; iIterator < iCount; iIterator++)
if (prop.Value[iIterator] is JObject)
Sort((JObject)prop.Value[iIterator]);
}
}
}
Cheers!
By using this approach you can retrieve a dynamic object with your json data
At the DynamicJsonConverter create a SortedDictionary instead
var d = new SortedDictionary<string, object>(dictionary);
// TODO: code to sort inner objects
return new DynamicJsonObject(d);
Then you can use
string jsonStr = "{\"B\":\"2\",\"A\":\"1\"}";
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
dynamic json = jss.Deserialize(jsonStr, typeof(object)) as dynamic;
string result = new JavaScriptSerializer().Serialize((json as DynamicJsonObject).Dictionary);
And result will have the expected output.