How can we loop through child node in JSON data in c#
I have written a c# code to pull the JSON data through HTTPREQUEST. The json data has multiple child node inside the parent node.
Below i have attached a sample JSON data format and the code which I built to loop through node. My code only check 1st level of child node (there will be child inside the child node).
Any one please help me to alter the code to loop through multiple child nodes.
2.Also I want apply delay of 1 mins after each web request.
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url.com/api/section);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
try
{
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}
foreach (forumsec.forumsec forumsec in JsonConvert.DeserializeObject<List<forumsec.forumsec>>(str4))
{
long id = forumsec.id;
TableOperation operation = TableOperation.InsertOrMerge((ITableEntity)new forumsecEntity(id.ToString())
{
pid= forumsec.pid,
name = forumsec.name,
});
HttpWebRequest httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://https://url.com/api/section?parentid=" + id.ToString());
httpWebRequest1.ContentType = "application/json";
httpWebRequest1.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest1.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
foreach (Childcategories forum in JsonConvert.DeserializeObject<List<Childcategories>>(str4))
{
long caid = forum.categoryid;
TableOperation operation1 = TableOperation.InsertOrMerge((ITableEntity)new ChildcategoriesEntity(caid.ToString())
{
pid = forum.pid,
name = forum.name
}
}
}
}
JSON file format
{
ID: 123
Name: BACD
ParentID:
Children: [
ID: 765
Name: atdr
ParentID: 123
Children: [
ID: 098
Name: jryr
ParentID: 765
Children:
]
]
c#
add a comment |
I have written a c# code to pull the JSON data through HTTPREQUEST. The json data has multiple child node inside the parent node.
Below i have attached a sample JSON data format and the code which I built to loop through node. My code only check 1st level of child node (there will be child inside the child node).
Any one please help me to alter the code to loop through multiple child nodes.
2.Also I want apply delay of 1 mins after each web request.
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url.com/api/section);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
try
{
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}
foreach (forumsec.forumsec forumsec in JsonConvert.DeserializeObject<List<forumsec.forumsec>>(str4))
{
long id = forumsec.id;
TableOperation operation = TableOperation.InsertOrMerge((ITableEntity)new forumsecEntity(id.ToString())
{
pid= forumsec.pid,
name = forumsec.name,
});
HttpWebRequest httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://https://url.com/api/section?parentid=" + id.ToString());
httpWebRequest1.ContentType = "application/json";
httpWebRequest1.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest1.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
foreach (Childcategories forum in JsonConvert.DeserializeObject<List<Childcategories>>(str4))
{
long caid = forum.categoryid;
TableOperation operation1 = TableOperation.InsertOrMerge((ITableEntity)new ChildcategoriesEntity(caid.ToString())
{
pid = forum.pid,
name = forum.name
}
}
}
}
JSON file format
{
ID: 123
Name: BACD
ParentID:
Children: [
ID: 765
Name: atdr
ParentID: 123
Children: [
ID: 098
Name: jryr
ParentID: 765
Children:
]
]
c#
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32
add a comment |
I have written a c# code to pull the JSON data through HTTPREQUEST. The json data has multiple child node inside the parent node.
Below i have attached a sample JSON data format and the code which I built to loop through node. My code only check 1st level of child node (there will be child inside the child node).
Any one please help me to alter the code to loop through multiple child nodes.
2.Also I want apply delay of 1 mins after each web request.
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url.com/api/section);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
try
{
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}
foreach (forumsec.forumsec forumsec in JsonConvert.DeserializeObject<List<forumsec.forumsec>>(str4))
{
long id = forumsec.id;
TableOperation operation = TableOperation.InsertOrMerge((ITableEntity)new forumsecEntity(id.ToString())
{
pid= forumsec.pid,
name = forumsec.name,
});
HttpWebRequest httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://https://url.com/api/section?parentid=" + id.ToString());
httpWebRequest1.ContentType = "application/json";
httpWebRequest1.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest1.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
foreach (Childcategories forum in JsonConvert.DeserializeObject<List<Childcategories>>(str4))
{
long caid = forum.categoryid;
TableOperation operation1 = TableOperation.InsertOrMerge((ITableEntity)new ChildcategoriesEntity(caid.ToString())
{
pid = forum.pid,
name = forum.name
}
}
}
}
JSON file format
{
ID: 123
Name: BACD
ParentID:
Children: [
ID: 765
Name: atdr
ParentID: 123
Children: [
ID: 098
Name: jryr
ParentID: 765
Children:
]
]
c#
I have written a c# code to pull the JSON data through HTTPREQUEST. The json data has multiple child node inside the parent node.
Below i have attached a sample JSON data format and the code which I built to loop through node. My code only check 1st level of child node (there will be child inside the child node).
Any one please help me to alter the code to loop through multiple child nodes.
2.Also I want apply delay of 1 mins after each web request.
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url.com/api/section);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
try
{
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}
foreach (forumsec.forumsec forumsec in JsonConvert.DeserializeObject<List<forumsec.forumsec>>(str4))
{
long id = forumsec.id;
TableOperation operation = TableOperation.InsertOrMerge((ITableEntity)new forumsecEntity(id.ToString())
{
pid= forumsec.pid,
name = forumsec.name,
});
HttpWebRequest httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://https://url.com/api/section?parentid=" + id.ToString());
httpWebRequest1.ContentType = "application/json";
httpWebRequest1.Method = "GET";
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest1.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str4 = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
foreach (Childcategories forum in JsonConvert.DeserializeObject<List<Childcategories>>(str4))
{
long caid = forum.categoryid;
TableOperation operation1 = TableOperation.InsertOrMerge((ITableEntity)new ChildcategoriesEntity(caid.ToString())
{
pid = forum.pid,
name = forum.name
}
}
}
}
JSON file format
{
ID: 123
Name: BACD
ParentID:
Children: [
ID: 765
Name: atdr
ParentID: 123
Children: [
ID: 098
Name: jryr
ParentID: 765
Children:
]
]
c#
c#
edited Nov 25 '18 at 10:22
Sreepu
asked Nov 25 '18 at 10:16
SreepuSreepu
116
116
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32
add a comment |
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466514%2fhow-can-we-loop-through-child-node-in-json-data-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466514%2fhow-can-we-loop-through-child-node-in-json-data-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you have made 8 questions and never accepted an answer
– derloopkat
Nov 25 '18 at 10:43
Apologies, accepted the answer
– Sreepu
Nov 25 '18 at 10:56
Can't you get the full json output at the first http request?
– Vishantha Peiris
Nov 25 '18 at 12:11
@VishanthaPeiris No, I was getting data of top node. I solved issue by looping the process until last node.
– Sreepu
Nov 28 '18 at 17:32