What is the simplest C# function to parse a JSON string into an object?
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(YourObjectType));
YourObjectType yourObject = (YourObjectType)serializer.ReadObject(jsonStream);
You could also use the JavaScriptSerializer
, but DataContractJsonSerializer
is supposedly better able to handle complex types.
Oddly enough JavaScriptSerializer was once deprecated (in 3.5) and then resurrected because of ASP.NET MVC (in 3.5 SP1). That would definitely be enough to shake my confidence and lead me to use DataContractJsonSerializer
since it is hard baked for WCF.
Convert JSON String To C# Object
It looks like you're trying to deserialize to a raw object. You could create a Class that represents the object that you're converting to. This would be most useful in cases where you're dealing with larger objects or JSON Strings.
For instance:
class Test {
String test;
String getTest() { return test; }
void setTest(String test) { this.test = test; }
}
Then your deserialization code would be:
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Test routes_list =
(Test)json_serializer.DeserializeObject("{ \"test\":\"some data\" }");
More information can be found in this tutorial:
http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-Csharp.aspx
Convert JSON String to JSON Object c#
JObject
defines method Parse
for this:
JObject json = JObject.Parse(str);
You might want to refer to Json.NET documentation.
Parse Json string in C#
I'm using Json.net in my project and it works great. In you case, you can do this to parse your json:
EDIT: I changed the code so it supports reading your json file (array)
Code to parse:
void Main()
{
var json = System.IO.File.ReadAllText(@"d:\test.json");
var objects = JArray.Parse(json); // parse as array
foreach(JObject root in objects)
{
foreach(KeyValuePair<String, JToken> app in root)
{
var appName = app.Key;
var description = (String)app.Value["Description"];
var value = (String)app.Value["Value"];
Console.WriteLine(appName);
Console.WriteLine(description);
Console.WriteLine(value);
Console.WriteLine("\n");
}
}
}
Output:
AppName
Lorem ipsum dolor sit amet
1
AnotherAppName
consectetur adipisicing elit
String
ThirdAppName
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
Text
Application
Ut enim ad minim veniam
100
LastAppName
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat
ZZZ
BTW, you can use LinqPad to test your code, easier than creating a solution or project in Visual Studio I think.
Convert string to json using C#
You can do something like this:
var jobject = JsonConvert.DeserializeObject<JObject>(yourVariable);
this is using Newtonsoft's json library that you can get from nuget.
Also JObject is the C# equivalent to a JSON object so that's probably something you'll want to use.
Parse JSON String to JSON Object in C#.NET
use new JavaScriptSerializer().Deserialize<object>(jsonString)
You need System.Web.Extensions dll and import the following namespace.
Namespace: System.Web.Script.Serialization
for more info MSDN
Json string to c# object
The following structure should get you started:
public class ReceiptCR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}
public class ReceiptDR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}
public class PaymentDR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}
And then define the wrapper:
public class Root
{
public ReceiptCR Receipt_cr { get; set; }
public ReceiptDR Receipt_dr { get; set; }
public PaymentDR Payment_dr { get; set; }
}
which you could deserialize from the json string:
string json = ...
Root result = JsonConvert.DeserializeObject<Root>(json);
Parsing json objects
Create a new class that your JSON can be deserialized into such as:
public class UserInfo
{
public string single_token { get; set; }
public string username { get; set; }
public string version { get; set; }
}
public partial class Downloader : Form
{
public Downloader(string url, bool showTags = false)
{
InitializeComponent();
WebClient client = new WebClient();
string jsonURL = "http://localhost/jev";
source = client.DownloadString(jsonURL);
richTextBox1.Text = source;
JavaScriptSerializer parser = new JavaScriptSerializer();
var info = parser.Deserialize<UserInfo>(source);
// use deserialized info object
}
}
Related Topics
Populate Treeview from List of File Paths in Wpf
Gui and Windows Service Communication
How to Open an Excel File in C#
How to Add an Index Field to Linq Results
Render Razor View to String in ASP.NET Core
Print Existing PDF (Or Other Files) in C#
Command to Close an Application of Console
What Is the Simplest C# Function to Parse a JSON String into an Object
Generically Flatten JSON Using C#
Count Property VS Count() Method
How to Check If a List Is Ordered
C# Drag Drop Does Not Work on Windows 7