Javascript 对象数组的 JSON.parse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11468806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:57:19  来源:igfitidea点击:

JSON.parse for array of object

javascriptjson

提问by

Server returns the array of object in JSON. It looks so:

服务器以 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)\/\"}
]"}

I need to parse using JSON.parse function. I'm doing this this way:

我需要使用 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?
      }

This code is not working, it returns undefined

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

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

回答by

This works perfectly

这完美地工作

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

But this doens't

但这不

    function myFunction(dataFromServer){
           var parsedJSON = JSON.parse(dataFromServer.d);
           for (var item in parsedJSON) {
               alert(item.Id);
         }
 }

回答by Rich O'Kelly

You can just access them as you would any object:

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

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

If you wish to enumerate them to use somehow, have a look at this SO question.

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

回答by Eivind Eidheim Elseth

You can access them as you do oridinary javascript objects, that is either as item.idor item['id']

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

回答by Mani

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