如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13839865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse my json string in C#(4.0)using Newtonsoft.Json package?
提问by Saravanan
I am new to JSON.In my asp.net application i want to parse the json string.So, i have used Newtonsoft.Json package for reading and writing json data.Now, i can able to parse the simple json data.But now i have received some complex json data for parsing.So, i little bit struck on it.
我是 JSON 新手。在我的 asp.net 应用程序中,我想解析 json 字符串。所以,我使用 Newtonsoft.Json 包来读取和写入 json 数据。现在,我可以解析简单的 json 数据。但是现在我收到了一些用于解析的复杂 json 数据。所以,我对它有点印象深刻。
This is JSON Data:
这是 JSON 数据:
{
quizlist: [
{
QUIZ: {
'QPROP': [
{
'name': 'FB',
'intro': '',
'timeopen': '1347871440',
'timeclose': '1355733840',
'timelimit': '0',
'noofques': '5',
'QUESTION': {
'QUEPROP': [
{
'questiontext': 'Scienceisbasedont',
'penalty': '0.3333333',
'qtype': 'shortanswer',
'answer': 'cause-and-effect',
'mark' : '5',
'hint': ''
},
{
'questiontext': 'otherscientistsevaluateit',
'penalty': '0.3333333',
'qtype': 'shortanswer',
'answer': 'Peerreview',
'mark' : '5',
'hint': ''
},
{
'questiontext': 'Watchingavariety',
'penalty': '0.3333333',
'qtype': 'shortanswer',
'answer': 'inductive',
'mark' : '5',
'hint': ''
},
{
'questiontext': 'coveriesorideas',
'penalty': '0.3333333',
'qtype': 'shortanswer',
'answer': 'paradigmshift',
'mark' : '5',
'hint': ''
},
{
'questiontext': 'proportions',
'penalty': '0.3333333',
'qtype': 'shortanswer',
'answer': 'fixed',
'mark' : '5',
'hint': ''
}
]
}
}
]
}
}
]
}
This is my C# Code :
这是我的 C# 代码:
dynamic dynObj = JsonConvert.DeserializeObject(jsonString);
foreach (var data in dynObj.quizlist)
{
foreach (var data1 in data.QUIZ.QPROP)
{
Response.Write("Name" + ":" + data1.name + "<br>");
Response.Write("Intro" + ":" + data1.intro + "<br>");
Response.Write("Timeopen" + ":" + data1.timeopen + "<br>");
Response.Write("Timeclose" + ":" + data1.timeclose + "<br>");
Response.Write("Timelimit" + ":" + data1.timelimit + "<br>");
Response.Write("Noofques" + ":" + data1.noofques + "<br>");
}
}
I can able to parse until noofques object in QPROP array objects.Now have to parse data.QUIZ.QPROP.QUESTION.QUEPROP array objects also...
我可以解析到 QPROP 数组对象中的 noofques 对象。现在还必须解析 data.QUIZ.QPROP.QUESTION.QUEPROP 数组对象...
But i failed to parse fully...
但我未能完全解析...
Please guide me to get out of this issue...
请指导我摆脱这个问题......
采纳答案by L.B
foreach (var data in dynObj.quizlist)
{
foreach (var data1 in data.QUIZ.QPROP)
{
Response.Write("Name" + ":" + data1.name + "<br>");
Response.Write("Intro" + ":" + data1.intro + "<br>");
Response.Write("Timeopen" + ":" + data1.timeopen + "<br>");
Response.Write("Timeclose" + ":" + data1.timeclose + "<br>");
Response.Write("Timelimit" + ":" + data1.timelimit + "<br>");
Response.Write("Noofques" + ":" + data1.noofques + "<br>");
foreach (var queprop in data1.QUESTION.QUEPROP)
{
Response.Write("Questiontext" + ":" + queprop.questiontext + "<br>");
Response.Write("Mark" + ":" + queprop.mark + "<br>");
}
}
}
回答by Vlad
You can use this tool to create appropriate c# classes:
您可以使用此工具创建适当的 c# 类:
http://jsonclassgenerator.codeplex.com/
http://jsonclassgenerator.codeplex.com/
and when you will have classes created you can simply convert string to object:
当您创建类时,您可以简单地将字符串转换为对象:
public static T ParseJsonObject<T>(string json) where T : class, new()
{
JObject jobject = JObject.Parse(json);
return JsonConvert.DeserializeObject<T>(jobject.ToString());
}
Here that classes: http://ge.tt/2KGtbPT/v/0?c
在这里上课:http: //ge.tt/2KGtbPT/v/0?c
Just fix namespaces.
只需修复命名空间。
回答by Donald
You could create your own class of type Quiz and then deserialize with strong type:
您可以创建自己的 Quiz 类型类,然后使用强类型反序列化:
Example:
例子:
quizresult = JsonConvert.DeserializeObject<Quiz>(args.Message,
new JsonSerializerSettings
{
Error = delegate(object sender1, ErrorEventArgs args1)
{
errors.Add(args1.ErrorContext.Error.Message);
args1.ErrorContext.Handled = true;
}
});
And you could also apply a schema validation.
您还可以应用架构验证。
回答by Prateek Gupta
This is a simple example of JSON parsing by taking example of google map API. This will return City name of given zip code.
这是一个简单的JSON解析示例,以google map API为例。这将返回给定邮政编码的城市名称。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Net;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
WebClient client = new WebClient();
string jsonstring;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
jsonstring = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address="+txtzip.Text.Trim());
dynamic dynObj = JsonConvert.DeserializeObject(jsonstring);
Response.Write(dynObj.results[0].address_components[1].long_name);
}
}
}

