开发者

ASP.NET - JSON casting

var a = JSON.Deserialize<object>(e.ExtraParams["address"]);

The JSON

[{"id":"","country":"MYcOUNTRY","city":"citycitycity","street":"street","block":"house","building":"building","additionalInfo":"fdsfdsfdsfds","latitude":"32.9206000","longitude":"35.1003000"}]

and the class for storing information about address

[Serializable]
 class BEAddress{
     public string Id { get; set; }
     public string Country { get; set; }
     public string City { get; set; }
     public string Street { get; set; }
     public string Block { get; set; }
     public string Building { get; set; }

     public string Latitude { get; set; }
     public string Longitude { get; set; }
     public string AdditionalInfo { get; set; }
    }

I can't savvy how to cast the object a to BEAddress?

UPDATE: The way I seriliaze this data

<script type="text/javascript">
          var <%=ClientID%>_getAddress = function () {
              var jsonObj = [];
              jsonObj.push({
                      Id: <%=AddressIdHd开发者_开发技巧n.ClientID%>.getValue(),
                      Country: <%=cmbCountries.ClientID%> .getRawValue(),
                      City: <%=cmbCities.ClientID%> .getRawValue(),
                      Street: <%=txtStreet.ClientID%> .getValue(),
                      Block: <%=txtBlock.ClientID%> .getValue(),
                      Building: <%=txtBuilding.ClientID%> .getValue(),
                      AdditionalInfo: <%=txtAdditionalInfo.ClientID%> .getValue(),
                      Latitude: <%=txtLatitude.ClientID%> .getValue(),
                      Longitude: <%=txtLongitude.ClientID%> .getValue()
                  });
              return jsonObj;
          }
</script>


I would imagine you'd need to do the following:

BEAddress aVar = JSON.Deserialize<BEAddress>(e.ExtraParams["address"]);

At the moment you're turning your JSON into a bog standard 'object' so you'd need to turn the JSON into a specific 'BEAddress' object as per the example above.


What happens if you try the following:

var a = JSON.Deserialize<BEAddress>(e.ExtraParams["address"]);

What you're currently doing is deserializing into an generic object whereas you need a BEAddress.

Also place the [Serializable] attribute onto BEAddress

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜