JavaScriptSerializer,从Http Request解析Json,将部件放入要显示的表中

时间:2012-06-14 15:58:37

标签: c# asp.net json parsing javascriptserializer

我打电话给名为Sample ApI的Web服务器。我希望能够解析json或xml中的数据,或者很好并以表格格式显示它。这就是我到目前为止所拥有的。

我想要的是能够解析此字符串_responseAsString中的数据并显示表格。我不知道如何启动它,我只知道JavaScriptSerialzer parseXXX = new Java...lizer()。请帮助我或协助正确的方向。

      public class Event
{
    public string event_key { get; set; }
    public string user_token { get; set; }
    public string event_set_key { get; set; }
    public string event_type { get; set; }
    public string event_date { get; set; }
    public string event_amount { get; set; }
    public string event_location_key { get; set; }
    public string event_location_name { get; set; }
    public string event_location_city { get; set; }
    public string event_location_state { get; set; }
    public string event_location_country { get; set; }
    public string event_acknowledged { get; set; }
}

     public ActionResult GetEvent()
     {
        try
        {

            string at = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
            string et = "KI2XfwQNByLPFdK4i3a74slLT7sjjzYRi9RR7zEtCoQ%3D";
            string t = "20111128183020";
            string _checkingUrl = String.Format("http://172.22.22.10/SampleAPI/Event/GetEvents?at={0}&et={1}&t={2}&responseFormat=json", at, et, t);
            System.Net.HttpWebRequest request=System.Net.WebRequest.Create(_checkingUrl) as System.Net.HttpWebRequest;
            System.Net.HttpWebResponse response=request.GetResponse() as System.Net.HttpWebResponse;
            System.IO.StreamReader _readResponse=new System.IO.StreamReader(response.GetResponseStream());
            //The encrypted dynamics response in either xml or json
            string _responseAsString=_readResponse.ReadToEnd();
            JavaScriptSerializer parseResponse = new JavaScriptSerializer();
            List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString);

         // this below is to make sure i was receiving my json data.       
     return Content(_responseAsString);


            _readResponse.Close();
        }
        catch (Exception e)
        {
            //log error
        }
        return View();
    }

这是我发出http请求时收到的json数据:

  

“[{\” event_key \ “:\” cc2a1802-2b04-4530-ad50-0d4f0ed19dd3 \ “\ ”user_token \“:\ ”40e62a11-40c4-408d-8cdd-1293cbaf9a41 \“,\” event_set_key \ “:\” 615017f2-ae28-4b8d-9def-cf043642b928 \ “\ ”EVENT_TYPE \“:\ ”到达\“,\ ”EVENT_DATE \“:\” 2011/6/20   四点15分28秒   PM \ “\ ”event_amount \“:\ ”100 \“,\ ”event_location_key \“:\ ”50fc1c22-d77b-4A91-b31d-da036827060b \“,\ ”event_location_name \“:\ ”商店2 \“,\” event_location_city \ “:\” 匹兹堡\”,\ “event_location_state \”:\ “PA \”,\ “event_location_country \”:\ “美\”,\ “event_location_lat \”:\ “\”,\ “event_location_long \” :\ “\” \ “EVENT_DESCRIPTION \”:\ “\” \ “event_acknowledged \”:\ “真\”},{\ “event_key \”:\ “2ac9e25e-137C-4a72-8cc5-157d67ea66c1 \” ,\ “user_token \”:\ “58cb4fcd-e140-4232-88c9-06eecb95b63d \”,\ “event_set_key \”:\ “00710ca7-f5d7-4c7a-BBFB-95491ae278ef \”,\ “EVENT_TYPE \”:\“到达\ “\ ”EVENT_DATE \“:\” 2011/9/23   四点15分28秒   PM \ “\ ”event_amount \“:\ ”45 \“,\ ”event_location_key \“:\ ”5a732dd5-9459-4cdd-A980-f3daf1a07343 \“,\ ”event_location_name \“:\ ”Store4 \“,\” event_location_city \ “:\” 匹兹堡\”,\ “event_location_state \”:\ “PA \”,\ “event_location_country \”:\ “美\”,\ “event_location_lat \”:\ “\”,\ “event_location_long \” :\ “\”,\ “EVENT_DESCRIPTION \”:\ “\”,\ “event_acknowledged \”:\ “假\”},{\ “event_key \”:\ “386b1fa1-11b2-48d9-b7f1-4bbe21ced487 \” ,\ “user_token \”:\ “c3d8b7ff-d85f-42a8-98f6-e091b48c2280 \”,\ “event_set_key \”:\ “dc55843b-f8cf-4e8a-9091-188ce0609fe1 \”,\ “EVENT_TYPE \”:\“到达\ “\ ”EVENT_DATE \“:\” 2011年9月18日   四点15分28秒   PM \ “\ ”event_amount \“:\ ”100 \“,\ ”event_location_key \“:\ ”be6d4fb4-c0e3-4303-b70d-7a22b721aa56 \“,\ ”event_location_name \“:\ ”商店1 \“,\” event_location_city \ “:\” 匹兹堡\”,\ “event_location_state \”:\ “PA \”,\ “event_location_country \”:\ “美\”,\ “event_location_lat \”:\ “\”,\ “event_location_long \” :\ “\” \ “EVENT_DESCRIPTION \”:\ “\” \ “event_acknowledged \”:\ “假\”}]“

2 个答案:

答案 0 :(得分:2)

JSON website有一些很好的信息。

对于旧版浏览器,您可以eval字符串(使用一些括号使其工作):

var myObject = eval('(' + myJsonText + ')');

现在,我们倾向于使用

JSON.parse(myJsonText);

服务器端,在C#中

var serializer = new JavaScriptSerializer();
T obj = serializer.Deserialize<T>(myJsonText);

答案 1 :(得分:1)

首先......

如果您只是传递从其他API返回的JSON消息,为什么不逐字地返回它们的响应字符串(换句话说,为什么要反序列化它)?

 public ActionResult GetEvent()
 {
        string at = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
        string et = "KI2XfwQNByLPFdK4i3a74slLT7sjjzYRi9RR7zEtCoQ%3D";
        string t = "20111128183020";
        string _checkingUrl = String.Format("http://172.22.22.10/SampleAPI/Event/GetEvents?at={0}&et={1}&t={2}&responseFormat=json", at, et, t);
        System.Net.HttpWebRequest request=System.Net.WebRequest.Create(_checkingUrl) as System.Net.HttpWebRequest;
        System.Net.HttpWebResponse response=request.GetResponse() as System.Net.HttpWebResponse;
        using (var readResponse= new StreamReader(response.GetResponseStream()))
        {
             return Content(readResponse.ReadToEnd(), "application/json");
        }
}

然后继续阅读

可以使用JSON文本阅读器将JSON消息拆分为名称/值对的表,但我认为在您的案例中忽略了这一点。如果消息是常量,只需创建一个表示JSON消息中每个元素的类并解析它。我用json2csharp来存根这样一个类:

public class Event
{
    public string event_key { get; set; }
    public string user_token { get; set; }
    public string event_set_key { get; set; }
    public string event_type { get; set; }
    public string event_date { get; set; }
    public string event_amount { get; set; }
    public string event_location_key { get; set; }
    public string event_location_name { get; set; }
    public string event_location_city { get; set; }
    public string event_location_state { get; set; }
    public string event_location_country { get; set; }
    public string event_location_lat { get; set; }
    public string event_location_long { get; set; }
    public string event_description { get; set; }
    public string event_acknowledged { get; set; }
}

然后使用您最喜欢的JSON序列化程序反序列化为这些对象的列表:

var serializer = new JavaScriptSerializer();
var events = serializer.Deserialize<List<Event>>(responseAsString);

(我更喜欢JSON.NET,这里等同于上面的块)

var events = JsonConvert.DeserializeObject<List<Event>>(responseAsString);

如果您确实需要能够阅读文本流并且通常创建名称/值对表,我将使用JSON.NETLINQ-to-JSON or the JsonTextReader