Click here to Skip to main content
15,391,989 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, bro.
Could anyone help me convert this python code to c#? I have to read the json data that comes to me from a url.
Thank you

What I have tried:

Python
def getHeader(token=None, content_type="application/json"):
    headers = {
        "Content-Type": content_type,
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
    }
    if token:
        headers.update({"Authorization": token})
    return headers


def getUserData(token):
    try:
        return loads(
            urlopen(Request("https://discordapp.com/api/v6/users/@me", headers=getHeader(token))).read().decode())
    except:
        pass
 
user_data = getUserData(token)
if not user_data:
   continue
username = user_data["username"]
user_id = user_data["id"]
email = user_data.get("email")
phone = user_data.get("phone")
Posted
Updated 30-Jun-22 4:46am
Comments
M Imran Ansari 12-Jan-22 6:20am
   
You can convert you Python code into C# using the link below link.
https://pythoncsharp.com/

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
   
Comments
Dargula87 12-Jan-22 4:44am
   
Thanks for the answer, I have the code in python and just want to know how to do the same for in a c # app that I am writing.
Examples of working code would be good. :)
There is no direct correlation between Python and C#, so do not waste your time trying to convert it. Instead take a look at How to serialize and deserialize JSON using C# - .NET | Microsoft Docs[^], which will get you to a solution much quicker.
   
Code Rewrite[^]

Learn C# & Json and then try, that's it.
   
solution...

var wc = new WebClient();
wc.Headers.Add("Content-Type", "application/json");
wc.Headers.Add(HttpRequestHeader.Authorization, match.ToString());
string ResultJSon = wc.DownloadString("https://discordapp.com/api/v8/users/@me");
wc.Dispose();

Thanks but I solved it by myself.
   
v2
namespace Namespace {

using System.Collections.Generic;

public static class Module {

public static object getHeader(object token = null, object content_type = "application/json") {
var headers = new Dictionary<object, object=""> {
{
"Content-Type",
content_type},
{
"User-Agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"}};
if (token) {
headers.update(new Dictionary<object, object=""> {
{
"Authorization",
token}});
}
return headers;
}

public static object getUserData(object token) {
try {
return loads(urlopen(Request("https://discordapp.com/api/v6/users/@me", headers: getHeader(token))).read().decode());
} catch {
}
}

public static object user_data = getUserData(token);

public static object username = user_data["username"];

public static object user_id = user_data["id"];

public static object email = user_data.get("email");

public static object phone = user_data.get("phone");
}
}
   
Comments
Dave Kreskowiak 30-Jun-22 11:21am
   
This is not a code conversion service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900