How to parse my json string in C#(4.0)using Newtonsoft.Json package?

前端 未结 4 481
温柔的废话
温柔的废话 2020-12-29 02:48

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

相关标签:
4条回答
  • 2020-12-29 02:59

    This is a simple example of JSON parsing by taking example of google map API. This will return City name of given zip code.

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 03:03
    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>");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 03:06

    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

    0 讨论(0)
  • 2020-12-29 03:08

    You can use this tool to create appropriate c# classes:

    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

    Just fix namespaces.

    0 讨论(0)
提交回复
热议问题