简体   繁体   English

如何解析 c# 中的 JSON?

[英]How do I parse JSON in c#?

Some time ago i post question about parsing JSON and I got a really good answer.前段时间我发布了关于解析 JSON 的问题,我得到了一个非常好的答案。 My json so far:到目前为止,我的 json:

{
  "stepLang": "en",
  "isFromCache": false,
  "isInNotebook": false,
  "standardQuery": "(-\\log_{2}(\\sqrt{2}))^{2}",
  "relatedProblems": [],
  "subject": "Algebra",
  "topic": "Algebra",
  "subTopic": "Simplify",
  "solutions": [
    {
      "step_input": "(-\\log_{2}(\\sqrt{2}))^{2}",
      "entire_result": "=\\frac{1}{4}",
      "solvingClass": "Solver",
      "isInterimStep": true,
      "isOpen": false,
      "isShowSolutionAfterStep": true,
      "title": {
        "text": {
          "createdText": "(-\\log_{2}(\\sqrt{2}))^{2}=\\frac{1}{4}\\quad(\\mathrm{Decimal:{\\quad}} 0.25)"
        }
      },
      "steps": [
        {
          "step_input": "\\log_{2}(\\sqrt{2})",
          "entire_result": "=(-\\frac{1}{2})^{2}",
          "isInterimStep": true,
          "isOpen": false,
          "isShowSolutionAfterStep": true,
          "title": {
            "text": {
              "createdText": "\\mathrm{Simplify} \\log_{2}(\\sqrt{2}):{\\quad}\\frac{1}{2}"
            }
          },
          "steps": [
            {
              "entire_result": "=\\log_{2}(2^{\\frac{1}{2}})",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "explanation": [
                {
                  "createdText": "\\mathrm{Rewrite as}"
                }
              ]
            },
            {
              "entire_result": "=\\frac{1}{2}\\log_{2}(2)",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "title": {
                "text": {
                  "createdText": "\\mathrm{Apply log rule }\\log_{a}(x^b)=b\\cdot\\log_{a}(x),\\quad\\mathrm{ assuming }x \\geq 0"
                }
              }
            },
            {
              "entire_result": "=\\frac{1}{2}",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "title": {
                "text": {
                  "createdText": "\\mathrm{Apply log rule}"
                }
              },
              "general_rule": {
                "text": {
                  "createdText": "\\log_a(a)=1"
                }
              },
              "practiceLink": "/practice/logarithms-practice",
              "practiceTopic": "Expand FOIL"
            }
          ]
        },
        {
          "step_input": "(-\\frac{1}{2})^{2}",
          "isInterimStep": true,
          "isOpen": false,
          "isShowSolutionAfterStep": true,
          "title": {
            "text": {
              "createdText": "\\mathrm{Simplify}"
            }
          },
          "steps": [
            {
              "entire_result": "=(\\frac{1}{2})^{2}",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "title": {
                "text": {
                  "createdText": "\\mathrm{Apply exponent rule}"
                }
              },
              "general_rule": {
                "text": {
                  "createdText": "(-a)^{n}=a^{n}, \\mathrm{if }n\\mathrm{ is even}"
                }
              },
              "explanation": [
                {
                  "createdText": "(-\\frac{1}{2})^{2}=(\\frac{1}{2})^{2}"
                }
              ]
            },
            {
              "entire_result": "=\\frac{1^{2}}{2^{2}}",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "title": {
                "text": {
                  "createdText": "\\mathrm{Apply exponent rule}"
                }
              },
              "general_rule": {
                "text": {
                  "createdText": "(\\frac{a}{b})^{c}=\\frac{a^{c}}{b^{c}}"
                }
              },
              "practiceLink": "/practice/exponent-practice",
              "practiceTopic": "Expand FOIL"
            },
            {
              "entire_result": "=\\frac{1}{2^{2}}",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "explanation": [
                {
                  "createdText": "\\mathrm{Apply rule} 1^{a}=1"
                },
                {
                  "createdText": "1^{2}=1"
                }
              ]
            },
            {
              "entire_result": "=\\frac{1}{4}",
              "isInterimStep": false,
              "isOpen": false,
              "isShowSolutionAfterStep": false,
              "explanation": [
                {
                  "createdText": "2^{2}=4"
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "dym": {
    "inputEquation": "\\left(-\\log _{2} \\sqrt{2}\\right)^{2}",
    "originalEquation": "(-\\log_{2}(\\sqrt{2}))^{2}",
    "outEquation": "(-\\log_{2}(\\sqrt{2}))^{2}",
    "dymEquation": "(-\\log_{2}(\\sqrt{2}))^{2}",
    "isTemplate": false,
    "showDidYouMean": false,
    "showInstead": false
  }
}

I got step_input and entire_result but i had problem with title field and explanation field.我得到了 step_input 和 whole_result 但标题字段和解释字段有问题。 So code for step_input and entire_result fields所以 step_input 和 whole_result 字段的代码

public class Root
{
    public List<Step> solutions { get; set; }

    public class Step
    {
        public string step_input { get; set; }
        public string entire_result { get; set; }
        // rest of properties
        public List<Step> steps { get; set; }
    }
}
var dynObj = JsonConvert.DeserializeObject<Root>(NotParsedResponse); // json above
foreach (var step in dynObj.solutions)
{
    if (step.steps != null)
    {
        foreach(var step1 in step.steps)
        {
            Result +=  step1.step_input + @" \\ " + step1.entire_result + @" \\ " ;
        }
    }
}

So i want to get title and general_rule fields.所以我想获得 title 和 general_rule 字段。 When i tried to get it with my code above it doesn't work.当我尝试使用上面的代码获取它时,它不起作用。

It is a bit difficult to help you because;帮助您有点困难,因为;

  1. You don't state the error你没有 state 错误
  2. You have omitted properties from your Root class which could be critical!!您从根 class 中省略了可能很关键的属性!

However, assuming that you don't have right set-up of your Root class then you need to add the following但是,假设您没有正确设置 Root class 那么您需要添加以下内容

Add the GeneralRule type into Root Add the new classes GeneralRule and MyText将 GeneralRule 类型添加到 Root 添加新类 GeneralRule 和 MyText

    public class Root
    {
        public List<Step> solutions { get; set; }

        public class Step
        {
            public string step_input { get; set; }
            public string entire_result { get; set; }

            //New entity below
            public GeneralRule general_rule { get; set; }
            public List<Step> steps { get; set; }
        }

        public class GeneralRule
        {
            public MyText text { get; set; }
        }

        public class MyText
        {
            public string createdText { get; set; }
        }
    }

Then your code should be OK.那么你的代码应该没问题。

In the future, I suggest to use a tool such as http://json2csharp.com/ to map your JSON to the right C# classes. In the future, I suggest to use a tool such as http://json2csharp.com/ to map your JSON to the right C# classes. Or use the Visual Studio option Edit->Paste Special->Past JSON as classes.或者使用 Visual Studio 选项 Edit->Paste Special->Past JSON 作为类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM