【问题标题】:Parse JSON Array using Newtonsoft使用 Newtonsoft 解析 JSON 数组
【发布时间】:2014-08-14 19:31:17
【问题描述】:

我正在从事一个项目,我想在其中反序列化 JSON 数组。我试过了,但不知道如何解析它。

JSON 数组:

{"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; }
}

【问题讨论】:

  • 我想要字符串中的值,我是新的,我不知道如何从 json 数组中获取这些值。
  • @DhavalPatel 感谢您的回复,但我怎样才能从"lec_no":"FA10-BCS-40","reg_no":"2","std_status":"A" 获得这些值 FA10-BCS-40, 2, A
  • 看我的答案我已经发布了

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


【解决方案1】:
RootObject ro = JsonConvert.DeserializeObject<RootObject>(strResult);

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

【讨论】:

    【解决方案2】:

    你可以这样使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多