解析雅虎!使用JavaScriptSerializer进行Placemaker JSON响应

时间:2012-01-07 16:11:38

标签: c# json javascriptserializer

我在解析从Yahoo!发送的JSON响应时遇到问题Placemaker。我只需要List LocalScopes。

我的课程定义如下:

public class PlacemakerResponse
{

    public Document document { get; set; }

    public class Document
    {
        public List<LocalScope> localScopes { get; set; }
    }

}

public class LocalScope
{

    public String woeId { get; set; }
    public String name { get; set; }
    public Centroid centroid { get; set; }

    public class Centroid
    {
        public String latitude { get; set; }
        public String longitude { get; set; }
    }

}

完整的JSON字符串如下。我在JSONLint验证了它,一切似乎都是订单。

{
    "processingTime": "0.00191",
    "version": " build 110725",
    "documentLength": "36",
    "document": {
        "0": {
            "placeDetails": {
                "placeId": "1",
                "place": {
                    "woeId": "483446",
                    "type": "Town",
                    "name": "Misida, Malta Majjistral, MT",
                    "centroid": {
                        "latitude": "35.8956",
                        "longitude": "14.4892"
                    }
                },
                "placeReferenceIds": "1",
                "matchType": "0",
                "weight": "1",
                "confidence": "10"
            }
        },
        "1": {
            "placeDetails": {
                "placeId": "2",
                "place": {
                    "woeId": "2450022",
                    "type": "Town",
                    "name": "Miami, FL, US",
                    "centroid": {
                        "latitude": "25.729",
                        "longitude": "-80.2374"
                    }
                },
                "placeReferenceIds": "2",
                "matchType": "0",
                "weight": "1",
                "confidence": "10"
            }
        },
        "administrativeScope": {
            "woeId": "0",
            "type": "Undefined",
            "name": "",
            "centroid": {
                "latitude": "0",
                "longitude": "0"
            }
        },
        "geographicScope": {
            "woeId": "1",
            "type": "Supername",
            "name": "Earth",
            "centroid": {
                "latitude": "0",
                "longitude": "0"
            }
        },
        "localScopes": [
            {
                "localScope": {
                    "woeId": "483446",
                    "type": "Town",
                    "name": "Misida, Malta Majjistral, MT (Town)",
                    "centroid": {
                        "latitude": "35.8956",
                        "longitude": "14.4892"
                    },
                    "southWest": {
                        "latitude": "35.8893",
                        "longitude": "14.472"
                    },
                    "northEast": {
                        "latitude": "35.9109",
                        "longitude": "14.4986"
                    },
                    "ancestors": [
                        {
                            "ancestor": {
                                "woeId": "24551414",
                                "type": "Locality",
                                "name": "Msida"
                            }
                        },
                        {
                            "ancestor": {
                                "woeId": "56043491",
                                "type": "Region",
                                "name": "Malta Majjistral"
                            }
                        },
                        {
                            "ancestor": {
                                "woeId": "23424897",
                                "type": "Country",
                                "name": "Malta"
                            }
                        }
                    ]
                }
            },
            {
                "localScope": {
                    "woeId": "2450022",
                    "type": "Town",
                    "name": "Miami, FL, US (Town)",
                    "centroid": {
                        "latitude": "25.729",
                        "longitude": "-80.2374"
                    },
                    "southWest": {
                        "latitude": "25.5403",
                        "longitude": "-80.4469"
                    },
                    "northEast": {
                        "latitude": "25.9578",
                        "longitude": "-80.028"
                    },
                    "ancestors": [
                        {
                            "ancestor": {
                                "woeId": "12587815",
                                "type": "County",
                                "name": "Miami-Dade"
                            }
                        },
                        {
                            "ancestor": {
                                "woeId": "2347568",
                                "type": "State",
                                "name": "Florida"
                            }
                        },
                        {
                            "ancestor": {
                                "woeId": "23424977",
                                "type": "Country",
                                "name": "United States"
                            }
                        }
                    ]
                }
            }
        ],
        "extents": {
            "center": {
                "latitude": "35.8956",
                "longitude": "14.4892"
            },
            "southWest": {
                "latitude": "25.5403",
                "longitude": "-80.4469"
            },
            "northEast": {
                "latitude": "35.9109",
                "longitude": "14.4986"
            }
        },
        "referenceList": [
            {
                "reference": {
                    "woeIds": "483446",
                    "placeReferenceId": "1",
                    "placeIds": "1",
                    "start": "20",
                    "end": "25",
                    "isPlaintextMarker": "1",
                    "text": "Msida",
                    "type": "plaintext",
                    "xpath": ""
                }
            },
            {
                "reference": {
                    "woeIds": "2450022",
                    "placeReferenceId": "2",
                    "placeIds": "2",
                    "start": "30",
                    "end": "35",
                    "isPlaintextMarker": "1",
                    "text": "Miami",
                    "type": "plaintext",
                    "xpath": ""
                }
            }
        ]
    }
}

当我这样做时:List<LocalScope> places = (ser.Deserialize<PlacemakerResponse>(stringJSON)).document.localScopes;

places列表有两个LocalScope对象,就像JSON字符串一样(到目前为止一样好),但是所有三个成员(woeId,name,centroid)都设置为null !!

我以前多次使用过JavaScriptSerializer,从来没有遇到任何问题,有没有人对我做错了什么建议?

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为你应该改变你的类声明如下

public class PlacemakerResponse
{

    public Document document { get; set; }

    public class Document
    {
        public List<AnotherPlacemaker> localScopes { get; set; }
    }

}

public class AnotherPlacemaker
{
    public LocalScope LocalScope;
}


public class LocalScope
{

    public String woeId { get; set; }
    public String name { get; set; }
    public Centroid centroid { get; set; }

    public class Centroid
    {
        public String latitude { get; set; }
        public String longitude { get; set; }
    }

}

如果您想使用Json.Net,您可以编写如下代码,而无需声明那些丑陋的对象

dynamic yahooObj =  JsonUtils.JsonObject.GetDynamicJsonObject(yourstring);
foreach (var item in yahooObj.document.localScopes)
{
    Console.WriteLine(
        item.localScope.woeId + " " + 
        item.localScope.name + " " + 
        item.localScope.centroid.latitude + " " + 
        item.localScope.centroid.longitude);
}