Igor Delovski Board Forum Index Igor Delovski Board
My Own Personal Slashdot!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C#

 
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Win32
Win32  
Author Message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3026
Location: Europe

PostPosted: Thu May 16, 2019 6:51 pm    Post subject: C# Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3026
Location: Europe

PostPosted: Thu May 16, 2019 7:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3026
Location: Europe

PostPosted: Thu May 16, 2019 7:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3026
Location: Europe

PostPosted: Thu May 16, 2019 7:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
delovski



Joined: 14 Jun 2006
Posts: 3522
Location: Zagreb

PostPosted: Sat May 18, 2019 3:12 pm    Post subject: Reply with quote

How to post JSON to a server using C#?

Code:
string json = new JavaScriptSerializer().Serialize(new
                {
                    user = "Foo",
                    password = "Baz"
                });
Back to top
View user's profile Send private message Visit poster's website
delovski



Joined: 14 Jun 2006
Posts: 3522
Location: Zagreb

PostPosted: Tue Jun 11, 2019 4:02 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
delovski



Joined: 14 Jun 2006
Posts: 3522
Location: Zagreb

PostPosted: Thu Jun 13, 2019 3:36 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
delovski



Joined: 14 Jun 2006
Posts: 3522
Location: Zagreb

PostPosted: Thu Jun 13, 2019 4:03 pm    Post subject: Reply with quote

How to: Examine and Instantiate Generic Types with Reflection

Code:
Type d1 = typeof(Dictionary<,>);
Console.WriteLine("   Is this a generic type? {0}",
    t.IsGenericType);
Console.WriteLine("   Is this a generic type definition? {0}",
    t.IsGenericTypeDefinition);
Back to top
View user's profile Send private message Visit poster's website
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3026
Location: Europe

PostPosted: Tue Sep 07, 2021 5:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Win32 All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Delovski.hr
Powered by php-B.B. © 2001, 2005 php-B.B. Group