-1

i have json like this

{\n\t\"username\":\"hunzla\",\n\t\"password\":\"123\"\n}

I want to get value of username "hunzla". How can i get it in c#?

Hunzla Ali
  • 383
  • 2
  • 5
  • 22
  • Deserialize the JSON string and access the properties. If you use Json.NET this looks like a simple: `var x=JObject.Parse(json); var name=x[useraname"];` Have you tried something? PS whitespace outside tokens in JSON is ignored so those tabs and newlines don't affect how you deserialize the string, – Panagiotis Kanavos Oct 14 '19 at 09:52
  • take a look at https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Nabi Sobhi Oct 14 '19 at 09:52
  • @Selvin the `active` tab in the linked question returns some pretty bad answers - both JavascriptSerializer and DataContractSeriazer are obsolete – Panagiotis Kanavos Oct 14 '19 at 09:56

1 Answers1

-1

use JavascriptSerializer

first step make a class

using System; using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class Userobject
{
    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("password")]
    public long Password { get; set; }
}

//use that class to Deserialize

JavaScriptSerializer js = new JavaScriptSerializer();  
BlogSites blogObject = js.Deserialize<Userobject>(jsonData);  
string name = blogObject.username ;  
string description = blogObject.password ; 
Negi Rox
  • 3,828
  • 1
  • 11
  • 18