如何在c#中解析JSON?
不久前,我发布了一个关于解析JSON的问题,我得到了一个非常好的答案。 到目前为止,我的目标是:如何在c#中解析JSON?,c#,json,C#,Json,不久前,我发布了一个关于解析JSON的问题,我得到了一个非常好的答案。 到目前为止,我的目标是: { "stepLang": "en", "isFromCache": false, "isInNotebook": false, "standardQuery": "(-\\log_{2}(\\sqrt{2}))^{2}", "relatedProblems": [], "subject": "Algebra", "topic": "Algebra", "subTop
{
"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
}
}
我得到了步骤输入和整个结果,但标题字段和解释字段有问题。
因此,步骤输入和整个结果字段的代码
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 + @" \\ " ;
}
}
}
公共类根目录
{
公共列表解决方案{get;set;}
公共类步骤
{
公共字符串步骤_输入{get;set;}
公共字符串整_结果{get;set;}
//其余物业
公共列表步骤{get;set;}
}
}
var dynObj=JsonConvert.DeserializeObject(NotParsedResponse);//上面的json
foreach(dynObj.解决方案中的var步骤)
{
if(step.steps!=null)
{
foreach(步骤中的var step1)
{
结果+=步骤1.步骤输入+@“\\”+步骤1.整个结果+@“\\”;
}
}
}
所以我想得到title和general_规则字段。
当我试图用上面的代码获取它时,它不起作用 帮助你有点困难,因为
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; }
}
}
公共类根目录
{
公共列表解决方案{get;set;}
公共类步骤
{
公共字符串步骤_输入{get;set;}
公共字符串整_结果{get;set;}
//下面是新实体
公共一般规则一般规则{get;set;}
公共列表步骤{get;set;}
}
公共类一般规则
{
公共MyText文本{get;set;}
}
公共类MyText
{
公共字符串createdText{get;set;}
}
}
那么你的代码应该是正确的
在将来,我建议使用一个工具,比如将JSON映射到正确的C#类。
或者使用Visual Studio选项Edit->Paste Special->Past JSON as Class。是的,但当我在代码中使用类似step1.general\u rule.text.createdText时,我得到的异常对象引用未设置为对象的实例。'Arg,连续4天出现相同问题。一切,而不是接近任何傻瓜谈论特殊或Json2CSharp。现在整件事都被破坏了。