English 中文(简体)
Json C# : deserializing a changing content or a part json response
原标题:Json C# : deserializing a changing content or a piece of json response

我使用的是Api,不是每当根据所要求的地点作出同样的反应时,就返回。 某些地方有更详细的情况,有些内容有其他内容的属性。 由此而产生的序列化物体在不匹配时,并非每一次都会导致脱硫。 该项目的目标与全部内容对应,但只是其中一部分内容:中心。

{
   "place":{
      "woeid":12345,
      "placeTypeName":"State",
      "placeTypeName attrs":{
         "code":8
      },
      "name":"My Region",
      "country":"",
      "country attrs":{
         "type":"Country",
         "code":"XX"
      },
      "admin1":"My Region",
      "admin1 attrs":{
         "type":"Region",
         "code":""
      },
      "admin2":"",
      "admin3":"",
      "locality1":"",
      "locality2":"",
      "postal":"",
      "centroid":{
         "latitude":30.12345,
         "longitude":40.761292
      },
      "boundingBox":{
         "southWest":{
            "latitude":32.2799,
            "longitude":50.715958
         },
         "northEast":{
            "latitude":29.024891,
            "longitude":12.1234
         }
      },
      "areaRank":10,
      "popRank":0,
      "uri":"http://where.yahooapis.com",
      "lang":"en-US"
   }
}

一个人能够指出一种最佳方法,即把一部分内容而不是完全的对应措施(并非在同一地方的cent),或使改变的反应图谋脱轨。

我使用Stack C#序列izer,但欢迎所有主张。 感谢。

最佳回答

There s actually a few ways you can parse this using ServiceStack s JsonSerializer as can be seen in this example of parsing one of GitHub s JSON API.

我将采用JsonObject办法,因为你即将结束选择的C#类,尽管这确实需要你与JsonSerializer一道使用的1-liner。 由此形成的法典的任何方面:

Func<JsonObject, Centroid> toCentroid = map => 
    new Centroid(map.Get<decimal>("latitude"), map.Get<decimal>("longitude"));

var place = JsonObject.Parse(JsonCentroid)
    .Object("place")
    .ConvertTo(x => new Place
    {
        WoeId = x.Get<int>("woeid"),
        PlaceTypeName = x.Get(""),
        PlaceTypeNameAttrs = x.Object("placeTypeName attrs"),
        Name = x.Get("Name"),
        Country = x.Get("Country"),
        CountryAttrs = x.Object("country attrs"),
        Admin1 = x.Get("admin1"),
        Admin1Attrs = x.Object("admin1 attrs"),
        Admin2 = x.Get("admin2"),
        Admin3 = x.Get("admin3"),
        Locality1 = x.Get("locality1"),
        Locality2 = x.Get("locality2"),
        Postal = x.Get("postal"),

        Centroid = x.Object("centroid")
            .ConvertTo(toCentroid),

        BoundingBox = x.Object("boundingBox")
            .ConvertTo(y => new BoundingBox
            {
                SouthWest = y.Object("southWest").ConvertTo(toCentroid),
                NorthEast = y.Object("northEast").ConvertTo(toCentroid)
            }),

        AreaRank = x.Get<int>("areaRank"),
        PopRank = x.Get<int>("popRank"),
        Uri = x.Get("uri"),
        Lang = x.Get("lang"),
    });

这里是

问题回答

You could use the DataContractJsonSerializer that s part of the standard .NET framework. You define just those attributes you re interested in. The other ones will be ignored.

class CentroidReader
{
    public static Centroid ReadControid(Stream stream)
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Response));
        Response response = serializer.ReadObject(stream) as Response;
        return response.Place.Centroid;
    }
}

[DataContract]
class Response
{
    [DataMember(Name = "place")]
    public Place Place { get; set; }
}

[DataContract]
class Place
{
    [DataMember(Name = "centroid")]
    public Centroid Centroid { get; set; }
}

[DataContract]
class Centroid
{
    [DataMember(Name = "latitude")]
    public double? Latitude { get; set; }
    [DataMember(Name = "longitude")]
    public double? Longitude { get; set; }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签