Win32 |
Author |
Message |
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Thu May 16, 2019 6:51 pm Post subject: C# |
|
|
Json.NET Documentation - JsonConvert Class
Code: | Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
|
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Thu May 16, 2019 7:00 pm Post subject: |
|
|
Accepting Raw Request Body Content with ASP.NET Web API
"I mentioned hints, and Web API allows you to use parameter binding
attributes to provide these hints. These allow you to tell Web API where
the content is coming from explicitly. There are [FromBody] and [FromUri]
attributes that can force content to be mapped from POST or query string
content for example." |
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Thu May 16, 2019 7:03 pm Post subject: |
|
|
How to access WebAPI from a .Net 3.5 client in C#
"Lets say, however, that we simply want to post some request (which may
have one or more request parameters of any type) that will return an object,
in this case a Product. First of all we must create an object that will store this
request" |
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Thu May 16, 2019 7:28 pm Post subject: |
|
|
cp - Json Parser, Viewer and Serializer
"I know that there are several Json parsers for C# but I like to understand
stuff by doing it on my own. So when I needed to explore some server output
in Json format and considering that Json really is a very simple structure, I
decided so create a custom parser reading it. This is the core of my tool to
show Json data in a tree view." |
|
Back to top |
|
|
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
|
Back to top |
|
|
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Tue Jun 11, 2019 4:02 pm Post subject: |
|
|
so - How do I create a Dictionary that holds different types in C#
You could use Dictionary<string, object> but then you'd need to cast the results:
Code: | int a = (int) Storage.Get("age");
string b = (string) Storage.Get("name");
double c = (double) Storage.Get("bmi");
Alternatively, you could make the Get method generic:
int a = Storage.Get<int>("age");
// etc
|
|
|
Back to top |
|
|
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Thu Jun 13, 2019 3:36 pm Post subject: |
|
|
so How can I parse JSON with C#?
Code: | // PM>Install-Package System.Json -Version 4.5.0
using System;
using System.Json;
namespace NetCoreTestConsoleApp
{
class Program
{
static void Main(string[] args)
{
// Note that JSON keys are case sensitive, a is not same as A.
// JSON Sample
string jsonString = "{\"a\": 1,\"b\": \"string value\",\"c\":[{\"Value\": 1}, {\"Value\": 2,\"SubObject\":[{\"SubValue\":3}]}]}";
// You can use the following line in a beautifier/JSON formatted for better view
// {"a": 1,"b": "string value","c":[{"Value": 1}, {"Value": 2,"SubObject":[{"SubValue":3}]}]}
/* Formatted jsonString for viewing purposes:
{
"a":1,
"b":"string value",
"c":[
{
"Value":1
},
{
"Value":2,
"SubObject":[
{
"SubValue":3
}
]
}
]
}
*/
// Verify your JSON if you get any errors here
JsonValue json = JsonValue.Parse(jsonString);
// int test
if (json.ContainsKey("a"))
{
int a = json["a"]; // type already set to int
Console.WriteLine("json[\"a\"]" + " = " + a);
}
// string test
if (json.ContainsKey("b"))
{
string b = json["b"]; // type already set to string
Console.WriteLine("json[\"b\"]" + " = " + b);
}
// object array test
if (json.ContainsKey("c") && json["c"].JsonType == JsonType.Array)
{
// foreach loop test
foreach (JsonValue j in json["c"])
{
Console.WriteLine("j[\"Value\"]" + " = " + j["Value"].ToString());
}
// multi level key test
Console.WriteLine("json[\"c\"][0][\"Value\"]" + " = " + json["c"][0]["Value"].ToString());
Console.WriteLine("json[\"c\"][0][\"Value\"]" + " = " + json["c"][1]["Value"].ToString());
Console.WriteLine("json[\"c\"][1][\"SubObject\"][0][\"SubValue\"]" + " = " + json["c"][1]["SubObject"][0]["SubValue"].ToString());
}
Console.WriteLine();
Console.Write("Press any key to exit.");
Console.ReadKey();
}
}
} |
|
|
Back to top |
|
|
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Tue Sep 07, 2021 5:08 pm Post subject: |
|
|
cp - How to use .NET C# COM objects in plain C
"This article is mostly for people who have program (written in C) already
created and for those who can't change the compiler from C to CLR because
of the problems that it may provide.
This article covers a basic setup for creation of communication between .NET
objects (through COM) to plain C (without any help from CLR and/or additional
frameworks). This can give you a possibility to call .NET (created in C# or VB)
methods, properties etc. directly from plain C project." |
|
Back to top |
|
|
|