Click here to Skip to main content
15,794,633 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:

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):
        return loads(
            urlopen(Request("", headers=getHeader(token))).read().decode())
user_data = getUserData(token)
if not user_data:
username = user_data["username"]
user_id = user_data["id"]
email = user_data.get("email")
phone = user_data.get("phone")
Updated 7-Jul-23 5:47am
M Imran Ansari 12-Jan-22 6:20am    
You can convert you Python code into C# using the link below link.

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.
Share this answer
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.
Share this answer
Code Rewrite[^]

Learn C# & Json and then try, that's it.
Share this answer

var wc = new WebClient();
wc.Headers.Add("Content-Type", "application/json");
wc.Headers.Add(HttpRequestHeader.Authorization, match.ToString());
string ResultJSon = wc.DownloadString("");

Thanks but I solved it by myself.
Share this answer

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