16

服务器以 JSON 格式返回对象数组。看起来是这样的:

{"d":"[
  {\"Id\":1,\"IsGood\":true,\"name1\":\"name1dsres\",\"Name2\":\"name2fdsfd\",\"name3\":  \"name3fdsgfd\",\"wasBorn\":\"\\/Date(284011000000)\\/\"},
  {\"Id\":2,\"IsGood\":false,\"name1\":\"fdsfds\",\"name2\":\"gfd3im543\",\"name3\":\"3543gfdgfd\",\"WasBorned\":\"\\/Date(281486800000)\\/\"}
]"}

我需要使用 JSON.parse 函数进行解析。我这样做是这样的:

   function myFunction(dataFromServer){
      var parsedJSON = JSON.parse(dataFromServer.d);
         for (var item in parsedJSON.d) {
          // how do I get the fields of current item?
      }

此代码不起作用,它返回未定义

for (var item in parsedJSON) {
      alert(item.Id);
}
4

4 回答 4

18

这完美地工作

    function myFunction(dataFromServer){
       var parsedJSON = JSON.parse(dataFromServer.d);
       for (var i=0;i<parsedJSON.length;i++) {
            alert(parsedJSON[i].Id);
         }
 }

但这并不

    function myFunction(dataFromServer){
           var parsedJSON = JSON.parse(dataFromServer.d);
           for (var item in parsedJSON) {
               alert(item.Id);
         }
 }
于 2012-07-13T10:59:43.637 回答
2

您可以像访问任何对象一样访问它们:

var id = item.Id;
if (item.IsGood) { ... }

如果您希望枚举它们以某种方式使用,请查看此 SO question

于 2012-07-13T10:38:10.283 回答
0

您可以像访问普通 javascript 对象一样访问它们,即作为item.iditem['id']

于 2012-07-13T10:40:47.697 回答
0
 class Program
{
    static void Main(string[] args)
    {

        var jsonString = @"{
                              ""data"": [
                                  {
                                    ""uid"": ""100001648098091"",
                                    ""first_name"": ""Payal"",
                                    ""last_name"": ""Sinha"",
                                    ""sex"": ""female"",
                                    ""pic_big_with_logo"": ""https://m.ak.fbcdn.net/external.ak/safe_image.php?d=AQAi8VLrTMB-UUEs&bust=1&url=https%3A%2F%2Fscontent-a.xx.fbcdn.net%2Fhprofile-ash2%2Fv%2Ft1.0-1%2Fs200x200%2F10018_433988026666130_85247169_n.jpg%3Foh%3Dc2774db94dff4dc9f393070c9715ef65%26oe%3D552CF366&logo&v=5&w=200&h=150"",
                                    ""username"": ""payal.sinha.505"",
                                  },
                                ]
                              }";
        dynamic userinfo = JValue.Parse(jsonString);

        IList<FacebookUserDetail> userDeatils = new List<FacebookUserDetail>();
        // 1st method
        foreach (dynamic userinfoItr in userinfo.data)
        {
            FacebookUserDetail userdetail= userinfoItr.ToObject<FacebookUserDetail>();
            userDeatils.Add(userdetail);


        }
        // 2nd Method
        var userDeatils1 = JsonConvert.DeserializeObject<FacebookUserDetails>(jsonString);
    }
}
public class FacebookUserDetail
{
    public string username { get; set; }
    //Password = EncryptionClass.Md5Hash(Guid.NewGuid().ToString()),                        
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string sex { get; set; }
    public string pic_big_with_log { get; set; }
}
enter code here
public class FacebookUserDetails
{
   public IList<FacebookUserDetail> data  { get; set; }
}
}
于 2019-12-25T04:59:03.093 回答