0

我调用了一个名为 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\":\"6/20/2011 4:15:28 PM\",\"event_amount \":\"100\",\"event_location_key\":\"50fc1c22-d77b-4a91-b31d-da036827060b\",\"event_location_name\":\"Store2\",\"event_location_city\":\"匹兹堡\",\"event_location_state\":\"PA\",\"event_location_country\":\"US\",\"event_location_lat\":\"\",\"event_location_long\":\"\",\ "事件描述\":\"\",\"event_acknowledged\":\"True\"},{\"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\":\"9/23/2011 4: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\":\"US\",\"event_location_lat\":\"\",\"event_location_long \":\"\",\"event_description\":\"\",\"event_acknowledged\":\"False\"},{\"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\" :\"9/18/2011 4:15:28 PM\",\"event_amount\":\"100\",\"event_location_key\":\"be6d4fb4-c0e3-4303-b70d-7a22b721aa56\",\ "event_location_name\":\"Store1\",\"event_location_city\":\"Pittsburgh\",\"event_location_state\":\"PA\",\"event_location_country\":\"US\",\"event_location_lat \":\"\",\"event_location_long\":\"\",\"event_description\":\"\",\"event_acknowledged\":\"False\"}]"

4

2 回答 2

2

JSON 网站对此有一些很好的信息。

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

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

而现在,我们倾向于使用

JSON.parse(myJsonText);

和服务器端,在 C# 中

var serializer = new JavaScriptSerializer();
T obj = serializer.Deserialize<T>(myJsonText);
于 2012-06-14T16:09:36.463 回答
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 或 JsonTextReader

于 2012-06-14T17:32:48.497 回答