javascript 对象数组的JSON.parse

2o7dmzc5  于 2023-04-04  发布在  Java
关注(0)|答案(5)|浏览(180)

服务器返回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?
      }

这段代码不起作用,它返回undefined

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

cgfeq70w1#

这个很好用

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

yzuktlbb2#

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

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

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

jdzmm42g

jdzmm42g3#

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

mftmpeh8

mftmpeh84#

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

ffdz8vbo5#

对象的键和值(如果是字符串,则为值)应该用双引号或单引号括起来。否则将发生解析错误

const jsonArr = '[{"id": 1, "name": "Tom"}, {"id": 2, "name": "Alice"}]';

const arr = JSON.parse(jsonArr);

console.log(arr);

相关问题