Link to home
Start Free TrialLog in
Avatar of Jesper Christensen
Jesper Christensen

asked on

Read json output with json.net (c#)

I have this json output.
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>");
}

Open in new window

But I want this outpub. How does I crawl the json string to get the output? Does anybody know?
MinimizeRenderBlockingResources:
(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)
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

sorry, what output you want to retrieve here?

as you got json below:

{
  "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.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"
          }
        ]
      }
    }
  }
}

Open in new window

Avatar of Ganesh Gurudu
Ganesh Gurudu

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_LITERAL\",\"key\":\"NUM_SCRIPTS\",\"value\":\"1\"},{\"type\":\"INT_LITERAL\",\"key\":\"NUM_CSS\",\"value\":\"3\"}]}
Avatar of Jesper Christensen

ASKER

Hi guys.

Thanks for your reply.

Maybe I did not make myself clear

if I run my code, I get this output:
MinimizeRenderBlockingResources
PrioritizeVisibleContent

Bud I would like this output:
MinimizeRenderBlockingResources:
(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.ruleResults.MinimizeRenderBlockingResources)
{
      Response.Write(item.Name+"<br>");
}
OR  like this

Response.Write(json.formattedResults.ruleResults(item).localizedRuleName+"<br>");
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
still not clear what do you mean by value 1 output? can you give a real example from your json value?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Saige THANKS !!!!!