I have following knockout text binding :
<td><strong><span id="texthotelcode" data-bind="text: parameters"
/></strong></td>
data binding of text: which returns data: {"id1":"2Z94","id2":"9861"}
now I want to convert them from this JSON into Key and value in Dictionary in C# as string, string
Any idea for this case thanks
.net can deserialize JSON in the following form into c# dictionary:
dict: [
{ "Key": "id1", "Value": "2Z94" },
{ "Key": "id2", "Value": "9861" }
]
So you can use a function like this one to convert your object:
function toDictionary(data) {
var dict = [];
for (var prop in data)
dict.push({ "Key": prop, "Value": data[prop] });
return dict;
}
Then just send this object to the server.
Note that as andyp pointed out, there is a similar question with answers in this thread.
When in search for an answer, please search the site first, and post your question only when no answer fits your needs. In this case, the other thread might need some update.
Related
I have a json which is badly formatted. I want to take out the status and order id from that json. Tried JSON parsing with object, but did not get the result. Please help,
My Json,
{
"formname": [
"Sale_Order_API",
{
"operation": [
"add",
{
"values": {
"Order_ID": "1250",
"Email": "xyz#yws.in",
"Order_Value": "100",
"Restaurant_Name": "HiTech",
"Order_Date": "13-Aug-2019",
},
"status": "Failure, Duplicate values found for
'Order ID'"
}
]
}
]
}
Please help.
This is my first question , please ignore mistakes.
I have tried something like this, But not able to get the inner values
dynamic resultdata = json_serializer.DeserializeObject(postData);
If I understand you correctly, you want to deserialize this JSON. On 'http://json2csharp.com/#' you can generate a C # class from your JSON. Or right by your own. There are plenty of tutorials on the Internet. In case your class, where you give the values of the Json, is called 'JSONResult', you could access the values as follows
var resultdata = JsonConvert.DeserializeObject<JSONResult>(postData);
JSONResult outPut = resultdata;
Console.WriteLine(outPut.formname[0]);
But the longer I look at the format of your JSON, the more confused I get. Where did you get the JSON from? From an API?
When I'm serialising to JSON, I'm used to .NET dictionaries becoming objects with the keys as properties and the values as their values. (The Json.Net docs have a succinct example.)
I'm using GraphQL and struggling to achieve a similar result. My data for this root query is basically a Dictionary<MyEnum,Dictionary<string,string>>. The closest I've got so far is this:
{
"data": {
"outterDict": [
{
"key": "THING_1",
"innerDict": [
{
"key": "key1",
"val": "val1"
},
...
]
},
{
"key": "THING_2",
"innerDict": [
{
"key": "key2",
"val": "val2"
},
...
]
}
]
}
}
But I want it to be closer to this:
{
"data": {
"collection": {
"THING_1": {
"key1": "val1",
...
},
"THING_2": {
"key2": "val2",
...
}
}
}
}
I'm struggling because GraphQL .Net only seems to understand lists, not dictionaries.
It doesn't feel like what I'm trying to do is unreasonable. Letting keys be keys seems right and like the most useful way to share this data (e.g. as suggested by this answer).
Is there a way to do this in GraphQL? Is there a way to do this in GraphQL .Net?
After quite of lot of research, it seems like this isn't in the spec of GraphQL and is therefore unsupported in GraphQL.Net.
GraphQL expects all possible properties (i.e. keys in a map/dictionary) to be known in advance. This means we lose a lot of the value of passing a dictionary at run time.
So although the GraphQL JSON is more verbose than idiomatic JSON, it's like this deliberately.
I have a JSON document coming from a vendor that looks like this:
{
"content": [{
"name": "Windows 8.1 x64",
"id": "Windows81x64",
"components": {
"Windows81x64": {
"propertyGroups": ["VirtualWindows81x64"],
"dependsOn": [],
"data": {
"provisioning_workflow": {
"fixed": {
"id": "WIMImageWorkflow",
"label": "WIMImageWorkflow"
}
},
"memory": {
"default": 2048,
"min": 2048,
"max": 16384
}
}
}
}
}]
}
Most of this document is fairly easy to deserialize into an object using the typical DataContractSerializer, however, there are a couple of keys/values that I am not sure what the "best practice" might be.
If you look at the "components" key the first key after that one is titled "Windows81x64". This key can change from document to document and it can be any value. It almost should be a 'Name' property of the collection but I can't control that. Furthermore, inside the 'Windows81x64' key there is another property called 'data'. According to the vendor the value of data is 'anonymous.' So, basically it can be anything.
Any ideas on the best way to deserialize this into a custom object when it comes to those parts of the document? Thank you.
You can deserialize dynamic ones as Dictionary<string, object>
Or if you know the value's type you can use Dictionary<string, ValueType> where the key of the dictionary would be the name (in your case Windows81x64)
I want to save a javascript object of arrays into a dictionary with a list of string in C# through ajax. But I can't figure out the format needed for the object in javascript.
My javascript object is in the format:
{
"foo" : ["abc", "def", "ghi"],
"foo2": ["123", "456", "789"],
}
But the C# data member ends up holding an empty object.
I've also tried
[
{"foo" : ["abc", "def", "ghi"]},
{"foo2": ["123", "456", "789"]},
]
My C# datamember is
[DataMember]
public Dictionary<string, List<string>> theDictionary;
I have no trouble with saving list of strings or saving other simple javascript objects if I've created the corresponding C# class. Any idea what format is needed?
so I found my answer here : Pass a javascript map to json wcf service
The format I needed was
[
{ Key: "foo" , Value: ["abc", "def", "ghi"]},
{ Key: "foo2", Value: ["123", "456", "789"]},
]
Before I get flagged for duplicate, I have the code from Dynamic json object with numerical keys working quite well now. The question with my numeric keys is that unfortunately, the JSON string I am getting is initially delimited by year, so would I use reflection to attempt to create a dynamic property on a dynamic object, and if so how? I know with a dynamic object I can't have obj["2010"] or obj[0]. In JavaScript this is no problem, just trying to get it working in C#. Ideas?
Example of JSON being returned:
{
"2010": [
{
"type": "vacation",
"alloc": "90.00"
},
Alternatively, sometimes the year is the second element as such:
I have no control over this json.
{
"year": [],
"2010": [
{
"type": "vacation",
"alloc": "0.00"
},
Maybe I'm misunderstanding your question, but here's how I'd do it:
static void Main(string[] args) {
var json = #"
{
'2010': [
{
'type': 'vacation',
'alloc': '90.00'
},
{
'type': 'something',
'alloc': '80.00'
}
]}";
var jss = new JavaScriptSerializer();
var obj = jss.Deserialize<dynamic>(json);
Console.WriteLine(obj["2010"][0]["type"]);
Console.Read();
}
Does this help?
I wrote a blog post on serializing/deserializing JSON with .NET: Quick JSON Serialization/Deserialization in C#
I have up-voted the question and JP's answer and am glad I dug around the internet to find this.
I have included a separate answer to simplify my use case for others to benefit from. The crux of it is:
dynamic myObj = JObject.Parse("<....json....>");
// The following sets give the same result
// Names (off the root)
string countryName = myObj.CountryName;
// Gives the same as
string countryName = myObj["CountryName"];
// Nested (Country capital cities off the root)
string capitalName = myObj.Capital.Name;
// Gives the same as
string capitalName = myObj["Capital"]["Name"];
// Gives the same as
string capitalName = myObj.Capital["Name"];
Now it all seems quite obvious but I just did not think of it.
Thanks again.