使用Newtonsoft解析JSON数组

时间:2014-06-24 13:44:54

标签: c# asp.net arrays json json.net

我正在开展一个项目,我希望反序列化一个JSON数组。我已经尝试但没有得到如何解析它。

JSON ARRAY:

{"showAttendanceResult":[{"lec_no":"FA10-BCS-40","reg_no":"2","std_status":"A","std_username":"fahidnadeem"},{"lec_no":"FA10-BCS-4","reg_no":"2","std_status":"A","std_username":"muneebamjad"}]}

这是我从WebService获得的JSON:

我是如何尝试的:

    string URL = "http://localhost:32319/ServiceEmployeeLogin.svc/getattendance";
    WebRequest wrGETURL;
    wrGETURL = WebRequest.Create(URL + "/" + Server.UrlEncode("24-06-2014"));
    wrGETURL.Method = "POST";
    wrGETURL.ContentType = @"application/json; charset=utf-8";
    HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;

    Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
    // read response stream from response object
    StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);

    // read string from stream data
    strResult = loResponseStream.ReadToEnd();
    // close the stream object
    loResponseStream.Close();
    // close the response object
    webresponse.Close();

    RootObject ro = JsonConvert.DeserializeObject<RootObject>(strResult);

    //what to do now?
    }
}

public class ShowAttendanceResult
{
    public string lec_no { get; set; }
    public string reg_no { get; set; }
    public string std_status { get; set; }
    public string std_username { get; set; }
}

public class RootObject
{
    public List<ShowAttendanceResult> showAttendanceResult { get; set; }
}

2 个答案:

答案 0 :(得分:3)

RootObject ro = JsonConvert.DeserializeObject<RootObject>(strResult);

foreach(var item in ro.showAttendanceResult)
{
    string _name= item.lec_no;
}

答案 1 :(得分:1)

您可以使用以下内容:

foreach(var item in ro.showAttendanceResult)
{
    string lec_no = item.lec_no;
}