【问题标题】:Parsing Yahoo! Placemaker JSON response using JavaScriptSerializer解析雅虎!使用 JavaScriptSerializer 的 Placemaker JSON 响应
【发布时间】:2012-01-07 16:11:37
【问题描述】:

我在解析从 Yahoo! 发送的 JSON 响应时遇到问题!占位符。我只需要 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&lt;LocalScope&gt; places = (ser.Deserialize&lt;PlacemakerResponse&gt;(stringJSON)).document.localScopes;

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

我之前已经多次使用 JavaScriptSerializer 并且从未遇到任何问题,有人对我做错了什么有任何建议吗?

谢谢。

【问题讨论】:

  • 如果你想使用 Json.Net 我可以提供答案
  • @L.B 我宁愿使用 JavaScriptSerialzer - 我在整个应用程序中都在使用它。你知道为什么这些值被保留为空吗?
  • @L.B 是的,这行得通!现在我明白我做错了什么。谢谢!
  • PlaceMaker 于 2012 年成为 PlaceSpotter。Yahoo! BOSS API(包括 PlaceSpotter)将于 2016 年 3 月 31 日关闭。如果您正在寻找替代品或替代品,您应该查看开源项目 CLAVIN 或最近推出的 Geoparser.io API。

标签: c# json javascriptserializer


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2012-08-24
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多