Introduction:
Here we will learn how to use jsontextwriter in c#,
vb.net
with example or how to write json using jsontextwriter in c#,
vb.net
with example or newtonsoft jsontextwriter to write json with example in c#,
vb.net
or convert list to json using newtonsoft jsontextwriter in c#,
vb.net
with example. By using newtonsoft reference in our applications we can easily
convert list to json format and serialize / de-serialize list objects easily
based on our requirements.
Description:
In previous articles I explained send / receive JSON objects from web service with example, angularjs filter json object to show / hide elements based on
conditions, Show info window on google maps when click on markers, show markers on google maps using json data, convert json string to json object with example and many
more articles related to in JSON,
asp.net,
mvc, c#,
vb.net.
Now I will explain how to use newtonsoft jsontextwriter to create json file
with required data in c#,
vb.net
with example.
Generally in our applications jsontextwriter is used to
convert the list items to json in required format and it will help us to show
list items as a separate json items based on our requirements.
Following is the simple code snippet to convert list
items to json format using jsontextwriter.
C#
Code
using System;
using
System.Collections.Generic;
using
System.Text;
using
Newtonsoft.Json;
using System.IO;
namespace
testtopics
{
class Program
{
static void Main(string[] args)
{
List<userdetails>
items = new List<userdetails>();
userdetails
user = new userdetails();
items.Add(new userdetails { userid = 1, username = "suresh",
location = "chennai" });
items.Add(new userdetails { userid = 2, username = "rohini",
location = "guntur" });
items.Add(new userdetails { userid = 3, username = "praveen", location = "bangalore" });
items.Add(new userdetails { userid = 4, username = "sateesh", location = "vizag" });
items.Add(new userdetails { userid = 5, username = "madhav",
location = "nagpur" });
items.Add(new userdetails { userid = 6, username = "honey",
location = "nagpur" });
StringBuilder
sb = new StringBuilder();
StringWriter
sw = new StringWriter(sb);
using(JsonTextWriter
jw = new JsonTextWriter(sw))
{
// jw.Formatting = Formatting.Indented;
foreach(var item in items)
{
jw.WriteStartObject();
var userinfo =
new userdetails
{
userid = item.userid,
username = item.username,
location = item.location
};
var proptypes
= userinfo.GetType();
jw.WritePropertyName("userid");
jw.WriteValue(userinfo.userid.ToString());
jw.WritePropertyName("username");
jw.WriteValue(userinfo.username.ToString());
jw.WritePropertyName("location");
jw.WriteValue(userinfo.location.ToString());
jw.WriteEndObject();
jw.WriteRaw("\n");
}
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
class userdetails
{
public int userid { get; set; }
public string username {
get; set; }
public string location {
get; set; }
}
}
|
VB.NET Code
Imports
System.Collections.Generic
Imports
System.Text
Imports
Newtonsoft.Json
Imports System.IO
Module Module1
Sub Main()
Dim items As New List(Of userdetails)()
Dim user As New userdetails()
items.Add(New userdetails() With {
.userid = 1,
.username = "suresh",
.location = "chennai"
})
items.Add(New userdetails() With {
.userid = 2,
.username = "rohini",
.location = "guntur"
})
items.Add(New userdetails() With {
.userid = 3,
.username = "praveen",
.location = "bangalore"
})
items.Add(New userdetails() With {
.userid = 4,
.username = "sateesh",
.location = "vizag"
})
items.Add(New userdetails() With {
.userid = 5,
.username = "madhav",
.location = "nagpur"
})
items.Add(New userdetails() With {
.userid = 6,
.username = "honey",
.location = "nagpur"
})
Dim sb As New StringBuilder()
Dim sw As New StringWriter(sb)
Using jw As New JsonTextWriter(sw)
'jw.Formatting = Formatting.Indented
For Each item In items
jw.WriteStartObject()
Dim userinfo =
New userdetails() With {
.userid = item.userid,
.username = item.username,
.location = item.location
}
Dim proptypes
= userinfo.[GetType]()
jw.WritePropertyName("userid")
jw.WriteValue(userinfo.userid.ToString())
jw.WritePropertyName("username")
jw.WriteValue(userinfo.username.ToString())
jw.WritePropertyName("location")
jw.WriteValue(userinfo.location.ToString())
jw.WriteEndObject()
jw.WriteRaw(vbLf)
Next
End Using
Console.WriteLine(sb.ToString())
Console.ReadLine()
End Sub
Class userdetails
Public Property userid() As Integer
Get
Return m_userid
End Get
Set
m_userid = Value
End Set
End Property
Private m_userid As Integer
Public Property username()
As String
Get
Return m_username
End Get
Set
m_username = Value
End Set
End Property
Private m_username
As String
Public Property location()
As String
Get
Return m_location
End Get
Set
m_location = Value
End Set
End Property
Private m_location
As String
End Class
End Module
|
If you observe above code we added namespace “Newtonsoft.JSON” this we can get by
adding reference using Manage Nuget
Packages. To add reference right click on References in your application Ã
select Manage Nuget Packages Ã
Go to Browse Tab Ã
Search for Newtonsoft Ã
From the list select Newtonsoft.Json
and install it. Once we install the component that will show like as shown
following.
Demo
When we run above code we will get result like as shown
below
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.