Hi ruben00000,
You need to use JavaScriptSerializer class to get the JSON string.
Please refer below sample.
HTML
<asp:Label ID="lblJson" runat="server" />
Namespace
C#
using System.Web.Script.Serialization;
VB.Net
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
List<SubResult> subResults = new List<SubResult>()
{
new SubResult() { RId = 1, Company="valid", Type="invalid"},
new SubResult() { RId = 2, Company="valid", Type="valid"}
};
List<Result> results = new List<Result>()
{
new Result() { Id = 1, Company="DEFGA", Type="Null" },
new Result() { Id = 2, Company="DEFGC", Type="Test" }
};
foreach (Result result in results)
{
List<SubResult> sr = new List<SubResult>();
foreach (SubResult subResult in subResults)
{
if (subResult.RId == result.Id)
{
sr.Add(subResult);
}
}
result.Checking = sr;
}
BaseJsonResponseUploadBulk rs = new BaseJsonResponseUploadBulk()
{
code = "1",
message = "success",
result = results
};
lblJson.Text = new JavaScriptSerializer().Serialize(rs);
}
}
public class BaseJsonResponseUploadBulk
{
public object code { set; get; }
public object message { set; get; }
public List<Result> result { get; set; }
}
public class Result
{
[ScriptIgnore]
public int Id { get; set; }
public string Company { get; set; }
public string Type { get; set; }
public List<SubResult> Checking { get; set; }
}
public class SubResult
{
[ScriptIgnore]
public int RId { get; set; }
public string Company { get; set; }
public string Type { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim subResults As List(Of SubResult) = New List(Of SubResult)() From {
New SubResult() With {
.RId = 1,
.Company = "valid",
.Type = "invalid"
},
New SubResult() With {
.RId = 2,
.Company = "valid",
.Type = "valid"
}
}
Dim results As List(Of Result) = New List(Of Result)() From {
New Result() With {
.Id = 1,
.Company = "DEFGA",
.Type = "Null"
},
New Result() With {
.Id = 2,
.Company = "DEFGC",
.Type = "Test"
}
}
For Each result As Result In results
Dim sr As List(Of SubResult) = New List(Of SubResult)()
For Each subResult As SubResult In subResults
If subResult.RId = result.Id Then
sr.Add(subResult)
End If
Next
result.Checking = sr
Next
Dim rs As BaseJsonResponseUploadBulk = New BaseJsonResponseUploadBulk() With {
.code = "1",
.message = "success",
.result = results
}
lblJson.Text = New JavaScriptSerializer().Serialize(rs)
End If
End Sub
Public Class BaseJsonResponseUploadBulk
Public Property code As Object
Public Property message As Object
Public Property result As List(Of Result)
End Class
Public Class Result
<ScriptIgnore()>
Public Property Id As Integer
Public Property Company As String
Public Property Type As String
Public Property Checking As List(Of SubResult)
End Class
Public Class SubResult
<ScriptIgnore()>
Public Property RId As Integer
Public Property Company As String
Public Property Type As String
End Class
Screenshot