asp.net解析json数组

时间:2014-08-06 13:36:51

标签: asp.net json

我在httphandler中写了这个用于读取json的代码:

var jsonSerilizer = new JavaScriptSerializer();
            var jsonString = String.Empty;
            context.Request.InputStream.Position = 0;
            using (var inputStream = new StreamReader(context.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }


我的json字符串是:

{"r_id":"140","name":"d","count":"5","c_id":"150"} 


我将此方法用于解析json字符串:

JavaScriptSerializer j = new JavaScriptSerializer();
            dynamic a = j.Deserialize(jsonString, typeof(object));
            string r_id = a["r_id"];
            string Name = a["name"];
            string count = a["count"];
            string c_id = a["c_id"];


代码将我的json字符串解析为:
r_id:140
名:d
数:5个
C_ID:50

当客户端向我发送字符串json数组时,例如:

{"r_id":"140","name":"d","count":"5","c_id":"150"}
{"r_id":"150","name":"der","count":"50","c_id":"150"}


我可以解析json字符串
我怎么样? 我使用这段代码:

 var jsonSerilizer = new JavaScriptSerializer();
            var jsonString = String.Empty;
            context.Request.InputStream.Position = 0;
            using (var inputStream = new StreamReader(context.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }
            File.AppendAllText(@"d:\status\LOL.txt", "GetJSON to FROM:"+ jsonString+"\r\n", Encoding.UTF8);
            JavaScriptSerializer j = new JavaScriptSerializer();
            dynamic a = j.Deserialize(jsonString, typeof(List<ClientMessage>));
            foreach (var obj in a)
            {
                File.AppendAllText(@"d:\status\LOL.txt", obj.name + "\r\n", Encoding.UTF8);
            }


但是当程序收到File.AppendAll ..程序崩溃并关闭时。

3 个答案:

答案 0 :(得分:0)

首先尝试创建一个模型(用于保存对象的类),如下所示:

Class ClientMessage {
           public string   r_id {get;set;}
          public string name {get;set;}
            public string count {get;set;}
            public string c_id {get;set;}
}

在这种情况下,您将收到一个List,因此请尝试这样做:

JavaScriptSerializer j = new JavaScriptSerializer();
            dynamic a = j.Deserialize(jsonString, typeof(List<ClientMessage>));

//然后迭代你的对象

foreach (var obj in a)
{
//start assigning values


}

答案 1 :(得分:0)

我建议创建一个可以解析JSON的对象,例如:

public MyClass {

    public int r_id { get; set;}
    public string name { get; set; }
    // etc
}

这将允许您直接解析到该对象,如下所示:

var results = j.Deserialize<List<MyClass>>(jsonString);

答案 2 :(得分:0)

你能尝试一下这段代码,我假设你正在获得如下的json数据。

[
    {
        r_id: "123",
        name: "deepu",
        count:"5",
        c_id:"150"
    },
    {
        r_id: "444",
        name: "aaa",
        count:"25",
        c_id:"55"
    },
    {
        r_id: "5467",
        name: "dfgdf",
        count:"5",
        c_id:"3434"
    }
]
  

我在这里使用上面的Client Message类并添加了JsonProperty属性。

public class ClientMessage
{
    [JsonProperty("r_id")]
    public string RId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("count")]
    public string Count { get; set; }

    [JsonProperty("c_id")]
    public string Cid { get; set; }
}

最后,获取json数据堡垒测试,我正在从外部文件中读取数据..

var data = System.IO.File.ReadAllText("jsondata.txt");
var jsResult = JsonConvert.DeserializeObject<List<ClientMessage>>(data);

注意:您需要在项目中添加Newtonsoft.Json引用