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...
请引导我离开这个问题……
4 个解决方案
#1
14
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>");
}
}
}
#2
9
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.
只是解决命名空间。
Regards
问候
#3
5
You could create your own class of type Quiz and then deserialize with strong type:
你可以创建自己的类型测试类,然后用强类型反序列化:
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.
您还可以应用模式验证。
http://james.newtonking.com/projects/json/help/index.html
http://james.newtonking.com/projects/json/help/index.html
#4
3
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解析示例,以谷歌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);
}
}
}
#1
14
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>");
}
}
}
#2
9
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.
只是解决命名空间。
Regards
问候
#3
5
You could create your own class of type Quiz and then deserialize with strong type:
你可以创建自己的类型测试类,然后用强类型反序列化:
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.
您还可以应用模式验证。
http://james.newtonking.com/projects/json/help/index.html
http://james.newtonking.com/projects/json/help/index.html
#4
3
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解析示例,以谷歌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);
}
}
}