Hi guys, while creating the JSON file we need to add an inner/separate node in most of the requirements. Just check the below 2 examples. Here I am using the "JsonTextWriter" class. WriteStartArray(), WriteEndArray()methods used to create inner node in JSON file.
Example 1:
JSON:
{
"CustGroup": "10", "Name": "Wholesales customers", "CustomerDetails": [ { "CustAccount": "US-003", "CustomerName": "Wholesales1" }, { "CustAccount": "US-004", "CustomerName": "Wholesales2" }, { "CustAccount": "US-006", "CustomerName": "Wholesales3" } ] }Code:
public static void main(Args _args)
{
System.IO.StringWriter stringWriter;
Newtonsoft.Json.JsonTextWriter jsonWriter;
CustTable custTable;
CustGroup custGroup;
stringWriter = new System.IO.StringWriter();
jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);
select firstonly custGroup;
str jsonString = "";
jsonWriter.WriteStartObject();// add '{'
jsonWriter.WritePropertyName("CustGroup");
jsonWriter.WriteValue(custGroup.CustGroup);
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(custGroup.Name);
// print customer which is attached to cust group.
jsonWriter.WritePropertyName("CustomerDetails");
jsonWriter.WriteStartArray();// add '['
while select custTable where custTable.CustGroup == custGroup.CustGroup
{
jsonWriter.WriteStartObject();// add '{'
jsonWriter.WritePropertyName("CustAccount");
jsonWriter.WriteValue(custTable.AccountNum);
jsonWriter.WritePropertyName("CustomerName");
jsonWriter.WriteValue(custTable.name());
jsonWriter.WriteEndObject();// add '}'
}
jsonWriter.WriteEndArray();// add ']'
jsonWriter.WriteEndObject();// add '}'
jsonString = stringWriter.ToString();
Info(strFmt("%1", stringWriter.ToString()));
}
Example 2:
JSON:
{
"CustGroup": "10",
"Name": "Wholesales customers",
"CustomerDetails": {
"CustAccount": "US-003",
"CustomerName": "Wholesales3"
}
}
Code:
public static void main(Args _args)
{
System.IO.StringWriter stringWriter;
Newtonsoft.Json.JsonTextWriter jsonWriter;
CustTable custTable;
CustGroup custGroup;
stringWriter = new System.IO.StringWriter();
jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);
select firstonly custGroup;
str jsonString = "";
jsonWriter.WriteStartObject();// add '{'
jsonWriter.WritePropertyName("CustGroup");
jsonWriter.WriteValue(custGroup.CustGroup);
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(custGroup.Name);
// print customer which is attached to cust group.
jsonWriter.WritePropertyName("CustomerDetails");
select custTable where custTable.CustGroup == custGroup.CustGroup;
jsonWriter.WriteStartObject();// add '{'
jsonWriter.WritePropertyName("CustAccount");
jsonWriter.WriteValue(custTable.AccountNum);
jsonWriter.WritePropertyName("CustomerName");
jsonWriter.WriteValue(custTable.name());
jsonWriter.WriteEndObject();// add '}'
jsonWriter.WriteEndObject();// add '}'
jsonString = stringWriter.ToString();
Info(strFmt("%1", stringWriter.ToString()));
}
Keep daxing!!
No comments:
Post a Comment