I am using this API and from this API I am getting following output.
{"apple.com":{"status":"regthroughothers","classkey":"dotin"},"microsoft.com":{"status":"regthroughothers","classkey":"dotin"},"asdfghq.in":{"status":"available","classkey":"dotin"}}
Note : here this output is depend on customer's input.
and Now here can you suggest me that how can I get this output in table format like
Try this I for example convert to DataTable
.
private string jsonObject = JSON_String.Replace("{", "[{").Replace("}", "}]");
public JsonExample()
{
JArray jArray = JArray.Parse(jsonObject);
DataTable dt = new DataTable();
dt.Columns.Add("St.No");
dt.Columns.Add("DomainName");
dt.Columns.Add("status");
dt.Columns.Add("classkey");
foreach (JProperty item in jArray[0])
{
var stNo= 0;
var jArray2 = JArray.Parse(item.Value.ToString());
foreach (var item2 in jArray2)
{
dt.Rows.Add(++stNo, item.Name, item2["status"], item2["classkey"]);
Console.WriteLine($" St. No: {stNo} DomainName: {item.Name} Status: {item2["status"]} classkey {item2["classkey"]} ");
}
}
}
You can use JSON.NET ( http://www.newtonsoft.com/json ). If you have never used JSON.NET, you can look at code samples in the link to get a feel: http://www.newtonsoft.com/json/help/html/SerializeObject.htm .
Considering the functionality in JSON.NET, the following code may not be the most elegant way but it gets your job done.
private class DomainProp
{
public string status;
public string classkey;
}
Assuming you have the class above:
string sJSON =@"{'apple.com':'status':'regthroughothers','classkey':'dotin'},
'microsoft.com':{'status':'regthroughothers','classkey':'dotin'},
'asdfghq.in':{'status':'available','classkey':'dotin'}}";
Dictionary<string, DomainProp> dicDomainProp = JsonConvert.DeserializeObject<Dictionary<string, DomainProp>>(sJSON);
// get into table format
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("S.No."),
new DataColumn("Domain Name"),
new DataColumn("Status"),
new DataColumn("Classkey") });
int i = 0;
foreach (KeyValuePair<string, DomainProp> pair in dicDomainProp)
dt.Rows.Add(++i, pair.Key, pair.Value.status, pair.Value.classkey);
Parsing the data is easy using Json.NET :
var input = "{ \"apple.com\":{ \"status\":\"regthroughothers\",\"classkey\":\"dotin\"},\"microsoft.com\":{ \"status\":\"regthroughothers\",\"classkey\":\"dotin\"},\"asdfghq.in\":{ \"status\":\"available\",\"classkey\":\"dotin\"} }";
dynamic data = Newtonsoft.Json.Linq.JObject.Parse(input);
This will return a list of objects whose Name
property is the domain name, and their Value
property contains status
and classkey
properties contains the contents of the respective fields.
By declaring the result as dynamic
we can use the dictionary keys as if they were properties of their object.
Rendering this as a table depends on the framework you are using. For example, one simple way to do this with ASP.NET MVC this is:
<table class="table">
<thead>
<tr>
<th>S.No.</th>
<th>Domain Name</th>
<th>Status</th>
<th>Class Key</th>
</tr>
</thead>
<tbody>
@{
int i = 1;
foreach (var it in data)
{
<tr>
<td>@(i++)</td>
<td>@it.Name</td>
<td>@it.Value.status</td>
<td>@it.Value.classkey</td>
</tr>
}
}
</tbody>
</table>
The result is :
<table> <thead> <tr> <th>S.No.</th> <th>Domain Name</th> <th>Status</th> <th>Class Key</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>apple.com</td> <td>regthroughothers</td> <td>dotin</td> </tr> <tr> <td>2</td> <td>microsoft.com</td> <td>regthroughothers</td> <td>dotin</td> </tr> <tr> <td>3</td> <td>asdfghq.in</td> <td>available</td> <td>dotin</td> </tr> </tbody> </table>
1 first convert your json into object form
Copy/pest your json object into json2sharp and copy/pest ModelObject into your code
2 Deserialize your json into modelobject
ModelObject object = new JavaScriptSerializer().Deserialize<ModelObject >(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.