Jesper Christensen
asked on
Read json output with json.net (c#)
I have this json output.
MinimizeRenderBlockingReso urces:
(Eliminate render-blocking JavaScript and CSS in above-the-fold content)
value 1 output
value 2 output
value 3 output
value 4 output
PrioritizeVisibleContent:
(Prioritize visible content)
var jsonstr = "{\"formattedResults\":{\"ruleResults\":{\"MinimizeRenderBlockingResources\":{\"localizedRuleName\":\"Eliminate render-blocking JavaScript and CSS in above-the-fold content\",\"ruleImpact\":12.0,\"groups\":[\"SPEED\"],\"summary\":{\"format\":\"Your page has {{NUM_SCRIPTS}} blocking script resources and {{NUM_CSS}} blocking CSS resources. This causes a delay in rendering your page.\",\"args\":[{\"type\":\"INT_LITERAL\",\"key\":\"NUM_SCRIPTS\",\"value\":\"1\"},{\"type\":\"INT_LITERAL\",\"key\":\"NUM_CSS\",\"value\":\"3\"}]},\"urlBlocks\":[{\"header\":{\"format\":\"None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML.\"}},{\"header\":{\"format\":\"{{BEGIN_LINK}}Remove render-blocking JavaScript{{END_LINK}}:\",\"args\":[{\"type\":\"HYPERLINK\",\"key\":\"LINK\",\"value\":\"https://developers.google.com/speed/docs/insights/BlockingJS\"}]},\"urls\":[{\"result\":{\"format\":\"{{URL}}\",\"args\":[{\"type\":\"URL\",\"key\":\"URL\",\"value\":\"value 1 output\"}]}}]},{\"header\":{\"format\":\"{{BEGIN_LINK}}Optimize CSS Delivery{{END_LINK}} of the following:\",\"args\":[{\"type\":\"HYPERLINK\",\"key\":\"LINK\",\"value\":\"https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery\"}]},\"urls\":[{\"result\":{\"format\":\"{{URL}}\",\"args\":[{\"type\":\"URL\",\"key\":\"URL\",\"value\":\"value 2 output\"}]}},{\"result\":{\"format\":\"{{URL}}\",\"args\":[{\"type\":\"URL\",\"key\":\"URL\",\"value\":\"value 3 output\"}]}},{\"result\":{\"format\":\"{{URL}}\",\"args\":[{\"type\":\"URL\",\"key\":\"URL\",\"value\":\"value 4 output\"}]}}]}]},\"PrioritizeVisibleContent\":{\"localizedRuleName\":\"Prioritize visible content\",\"ruleImpact\":.0,\"groups\":[\"SPEED\"],\"summary\":{\"format\":\"You have the above-the-fold content properly prioritized. Learn more about {{BEGIN_LINK}}prioritizing visible content{{END_LINK}}.\",\"args\":[{\"type\":\"HYPERLINK\",\"key\":\"LINK\",\"value\":\"https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent\"}]}}}}}";
dynamic json = JsonConvert.DeserializeObject<dynamic>(jsonstr);
foreach (var item in json.formattedResults.ruleResults)
{
Response.Write(item.Name+"<br>");
}
But I want this outpub. How does I crawl the json string to get the output? Does anybody know?MinimizeRenderBlockingReso
(Eliminate render-blocking JavaScript and CSS in above-the-fold content)
value 1 output
value 2 output
value 3 output
value 4 output
PrioritizeVisibleContent:
(Prioritize visible content)
Hello Christensen,
it seem your are doing wrong, is the code working? definitely it might have error.
it always work on name value pairs.
you may get output like this
localizedRuleName : Eliminate render-blocking JavaScript and CSS in above-the-fold content
ruleImpact: 12.0
groups:[SPEED]
summary:{\"format\":\"Your page has {{NUM_SCRIPTS}} blocking script resources and {{NUM_CSS}} blocking CSS resources. This causes a delay in rendering your page.\",\"args\":[{\"type\ ":\"INT_LI TERAL\",\" key\":\"NU M_SCRIPTS\ ",\"value\ ":\"1\"},{ \"type\":\ "INT_LITER AL\",\"key \":\"NUM_C SS\",\"val ue\":\"3\" }]}
it seem your are doing wrong, is the code working? definitely it might have error.
it always work on name value pairs.
you may get output like this
localizedRuleName : Eliminate render-blocking JavaScript and CSS in above-the-fold content
ruleImpact: 12.0
groups:[SPEED]
summary:{\"format\":\"Your
Go through the below URL for more understanding on this JSON.
http://www.c-sharpcorner.com/UploadFile/manas1/json-serialization-and-deserialization-using-json-net-librar/
http://www.c-sharpcorner.com/UploadFile/manas1/json-serialization-and-deserialization-using-json-net-librar/
ASKER
Hi guys.
Thanks for your reply.
Maybe I did not make myself clear
if I run my code, I get this output:
MinimizeRenderBlockingReso urces
PrioritizeVisibleContent
Bud I would like this output:
MinimizeRenderBlockingReso urces:
(Eliminate render-blocking JavaScript and CSS in above-the-fold content)
value 1 output
value 2 output
value 3 output
value 4 output
I can´t figure out how to loop through the json objects.
Thanks for your reply.
Maybe I did not make myself clear
if I run my code, I get this output:
MinimizeRenderBlockingReso
PrioritizeVisibleContent
Bud I would like this output:
MinimizeRenderBlockingReso
(Eliminate render-blocking JavaScript and CSS in above-the-fold content)
value 1 output
value 2 output
value 3 output
value 4 output
I can´t figure out how to loop through the json objects.
try using below code
Response.Write(item.Name+" <br>");
you need get into sub level like this. or use sub loop
foreach (var item in json.formattedResults.rule Results.Mi nimizeRend erBlocking Resources)
{
Response.Write(item.Name+" <br>");
}
OR like this
Response.Write(json.format tedResults .ruleResul ts(item).l ocalizedRu leName+"<b r>");
Response.Write(item.Name+"
you need get into sub level like this. or use sub loop
foreach (var item in json.formattedResults.rule
{
Response.Write(item.Name+"
}
OR like this
Response.Write(json.format
ASKER
Hi. Try insert my code to VS Studio? Then you will se, that your code returns the same as mine.
How do I loop through the json string and get the values I want? F.ex:
value 1 output
value 2 output
value 3 output
value 4 output
How do I loop through the json string and get the values I want? F.ex:
value 1 output
value 2 output
value 3 output
value 4 output
How do I loop through the json string and get the values I want? F.ex:still not clear what do you mean by value 1 output? can you give a real example from your json value?
value 1 output
value 2 output
value 3 output
value 4 output
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Saige THANKS !!!!!
as you got json below:
Open in new window