নিম্নলিখিত 2 টি উদাহরণ দুটি ব্যবহার করে
- System.Web.Script.Serialization বা এর অধীনে জাভাস্ক্রিপ্টসরিয়ালাইজার
- System.Web.Helpers এর অধীনে Json.Decode
উদাহরণ 1: System.Web.Script.Serialization ব্যবহার করে
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Script.Serialization;
namespace Tests
{
[TestClass]
public class JsonTests
{
[TestMethod]
public void Test()
{
var json = "{\"user\":{\"name\":\"asdf\",\"teamname\":\"b\",\"email\":\"c\",\"players\":[\"1\",\"2\"]}}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic jsonObject = serializer.Deserialize<dynamic>(json);
dynamic x = jsonObject["user"]; // result is Dictionary<string,object> user with fields name, teamname, email and players with their values
x = jsonObject["user"]["name"]; // result is asdf
x = jsonObject["user"]["players"]; // result is object[] players with its values
}
}
}
ব্যবহার: কাস্টম সি # অবজেক্টে জেএসওএন অবজেক্ট
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Script.Serialization;
using System.Linq;
namespace Tests
{
[TestClass]
public class JsonTests
{
[TestMethod]
public void TestJavaScriptSerializer()
{
var json = "{\"user\":{\"name\":\"asdf\",\"teamname\":\"b\",\"email\":\"c\",\"players\":[\"1\",\"2\"]}}";
User user = new User(json);
Console.WriteLine("Name : " + user.name);
Console.WriteLine("Teamname : " + user.teamname);
Console.WriteLine("Email : " + user.email);
Console.WriteLine("Players:");
foreach (var player in user.players)
Console.WriteLine(player);
}
}
public class User {
public User(string json) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<dynamic>(json);
name = (string)jsonObject["user"]["name"];
teamname = (string)jsonObject["user"]["teamname"];
email = (string)jsonObject["user"]["email"];
players = jsonObject["user"]["players"];
}
public string name { get; set; }
public string teamname { get; set; }
public string email { get; set; }
public Array players { get; set; }
}
}
উদাহরণ 2: System.Web.Helpers ব্যবহার করে
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Helpers;
namespace Tests
{
[TestClass]
public class JsonTests
{
[TestMethod]
public void TestJsonDecode()
{
var json = "{\"user\":{\"name\":\"asdf\",\"teamname\":\"b\",\"email\":\"c\",\"players\":[\"1\",\"2\"]}}";
dynamic jsonObject = Json.Decode(json);
dynamic x = jsonObject.user; // result is dynamic json object user with fields name, teamname, email and players with their values
x = jsonObject.user.name; // result is asdf
x = jsonObject.user.players; // result is dynamic json array players with its values
}
}
}
ব্যবহার: কাস্টম সি # অবজেক্টে জেএসওএন অবজেক্ট
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Helpers;
using System.Linq;
namespace Tests
{
[TestClass]
public class JsonTests
{
[TestMethod]
public void TestJsonDecode()
{
var json = "{\"user\":{\"name\":\"asdf\",\"teamname\":\"b\",\"email\":\"c\",\"players\":[\"1\",\"2\"]}}";
User user = new User(json);
Console.WriteLine("Name : " + user.name);
Console.WriteLine("Teamname : " + user.teamname);
Console.WriteLine("Email : " + user.email);
Console.WriteLine("Players:");
foreach (var player in user.players)
Console.WriteLine(player);
}
}
public class User {
public User(string json) {
var jsonObject = Json.Decode(json);
name = (string)jsonObject.user.name;
teamname = (string)jsonObject.user.teamname;
email = (string)jsonObject.user.email;
players = (DynamicJsonArray) jsonObject.user.players;
}
public string name { get; set; }
public string teamname { get; set; }
public string email { get; set; }
public Array players { get; set; }
}
}
এই কোডটির জন্য সিস্টেম যোগ করা দরকার e
% প্রোগ্রামফল%% মাইক্রোসফ্ট ASP.NET \ ASP.NET ওয়েব পৃষ্ঠা {সংস্করণ \ সমাবেশগুলি \ System.Web.Helpers.dll
অথবা
% প্রোগ্রামফায়ালস (x86)% \ মাইক্রোসফ্ট এএসপি.নেট \ এএসপি.নেট ওয়েব পৃষ্ঠাগুলি ERS সংস্করণ \ সমাবেশগুলি \ সিস্টেম. ওয়েবে.হেল্পার্স.ডিএল
আশাকরি এটা সাহায্য করবে!