如何反序列化不带名称的JSON数组? [英] How to deserialize a JSON array with no name?

查看:114
本文介绍了如何反序列化不带名称的JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何编写一个用于从.Net中的JSON反序列化对象列表的类.

I cannot figure out how to write a class for deserializing a list of objects from JSON in .Net.

从JSON规范中,我们了解到这是有效的JSON:

From the JSON spec, we learn that this is valid JSON:

   [
      {
         "precision": "zip",
         "Latitude":  37.7668,
         "Longitude": -122.3959,
         "Address":   "",
         "City":      "SAN FRANCISCO",
         "State":     "CA",
         "Zip":       "94107",
         "Country":   "US"
      },
      {
         "precision": "zip",
         "Latitude":  37.371991,
         "Longitude": -122.026020,
         "Address":   "",
         "City":      "SUNNYVALE",
         "State":     "CA",
         "Zip":       "94085",
         "Country":   "US"
      }    ]

所以我构造了这个类来处理反序列化:

So I constructed this class to handle deserialization:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace JsonRfc{

[Serializable]
public class Location {

    public string Precision;
    public double Latitude;
    public double Longitude;
    public string Address;
    public string City;
    public string State;
    public string Zip;
    public string Country;

    public Location(){}

    public static Location DeserializedJson(string responseJson)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        return jsSerializer.Deserialize<Location>(responseJson);
    }
}

[Serializable]
public class Locations {

    public List<Location> Location;

    public Locations(){}

    public static Locations DeserializedJson(string responseJson)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        return jsSerializer.Deserialize<Locations>(responseJson);
    }
}
}

尽管将有效的Json传递给该方法,反序列化的对象仍为null.

The deserialized object is null despite having valid Json passed to the method.

我尝试失败的其他事情包括:将Locations设置为数组而不是列表(因此:public Location [] Location;),并反序列化到location,即使Json包含一个locations数组.

Other things I tried that failed included: making Locations an array instead of a list (so: public Location[] Location; ), and deserializing to location even though the Json contains an array of locations.

那么,.Net开发人员应该如何反序列化对象数组?我希望以上方法能奏效,但不能奏效.

So, how is a .Net developer expected to deserialize an array of objects? I expected the above to work and it does not.

推荐答案

只需返回一个数组

var locs = Location.DeserializedJson(json);


public class Location
{

    public string Precision;
    public double Latitude;
    public double Longitude;
    public string Address;
    public string City;
    public string State;
    public string Zip;
    public string Country;

    public static Location[] DeserializedJson(string responseJson)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        return jsSerializer.Deserialize<Location[]>(responseJson);
    }
}

PS:请注意,不需要[Serializable].

这篇关于如何反序列化不带名称的JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆