从 WCF 服务获取地址名称 [英] get address name from WCF service

查看:24
本文介绍了从 WCF 服务获取地址名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 WCF 服务并在 http://kailun92wcf.cloudapp.net/中发布到云端Service1.svc.如何使用 getSearchCoords 的方法获取所有名称并将其显示到列表中?下面显示了一个示例测试结果.

I have created a WCF service and publish into the cloud in http://kailun92wcf.cloudapp.net/Service1.svc. How can I use the method of getSearchCoords to get all the names out and display it into a list ? One example tested result is display below.

"{\"SearchResults\":[{\"PageCount\":\"1\"},{\"SEARCHVAL\":\"ORCHARD 22\",\"CATEGORY\":\"Buildin" +
    "g\",\"X\":\"29483.4267\",\"Y\":\"31269.938\"},{\"SEARCHVAL\":\"ORCHARD BEL AIR\",\"CATEGORY\":\"" +
    "Building\",\"X\":\"27071.2616\",\"Y\":\"31629.2465\"},{\"SEARCHVAL\":\"ORCHARD BOULEVARD\",\"C" +
    "ATEGORY\":\"CATC\",\"X\":\"27614.8046\",\"Y\":\"31857.4392\"},{\"SEARCHVAL\":\"ORCHARD BUILDIN" +
    "G\",\"CATEGORY\":\"Building\",\"X\":\"28449.6799\",\"Y\":\"31527.587\"},{\"SEARCHVAL\":\"ORCHARD" +
    " BUILDING (FRIENDLY BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"28448.5715\",\"Y\":\"315" +
    "26.146\"},{\"SEARCHVAL\":\"ORCHARD BUILDING (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreat" +
    "ion\",\"X\":\"28448.3426\",\"Y\":\"31525.9693\"},{\"SEARCHVAL\":\"ORCHARD CENTRAL\",\"CATEGORY" +
    "\":\"Building\",\"X\":\"28709.3453\",\"Y\":\"31452.9157\"},{\"SEARCHVAL\":\"ORCHARD CENTRAL (F" +
    "RIENDLY BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"28709.3453\",\"Y\":\"31452.9157\"},{\"" +
    "SEARCHVAL\":\"ORCHARD CENTRAL (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreation\",\"X\":\"28" +
    "709.3453\",\"Y\":\"31452.9156\"},{\"SEARCHVAL\":\"ORCHARD CINELEISURE (WIRELESS HOTSPOTS" +
    ")\",\"CATEGORY\":\"Recreation\",\"X\":\"28347.9192\",\"Y\":\"31538.4923\"},{\"SEARCHVAL\":\"ORCH" +
    "ARD COURT\",\"CATEGORY\":\"Building\",\"X\":\"28931.3725\",\"Y\":\"31225.6489\"},{\"SEARCHVAL\"" +
    ":\"ORCHARD CREDIT AUTO HOUSE\",\"CATEGORY\":\"Building\",\"X\":\"23255.1398\",\"Y\":\"35016.5" +
    "269\"},{\"SEARCHVAL\":\"ORCHARD EMERALD (GREEN MARK BUILDINGS)\",\"CATEGORY\":\"Environm" +
    "ent\",\"X\":\"28617.7255\",\"Y\":\"31549.8898\"},{\"SEARCHVAL\":\"ORCHARD FOUNTAIN CORNER\",\"" +
    "CATEGORY\":\"Building\",\"X\":\"28464.8743\",\"Y\":\"31580.3349\"},{\"SEARCHVAL\":\"ORCHARD GA" +
    "TEWAY\",\"CATEGORY\":\"Building\",\"X\":\"28666.655\",\"Y\":\"31427.7293\"},{\"SEARCHVAL\":\"ORC" +
    "HARD GATEWAY @ EMERALD\",\"CATEGORY\":\"Building\",\"X\":\"28617.699\",\"Y\":\"31549.9633\"}," +
    "{\"SEARCHVAL\":\"ORCHARD GRAND COURT PTE LTD (FRIENDLY BUILDINGS)\",\"CATEGORY\":\"Comm" +
    "unity\",\"X\":\"28580.4218\",\"Y\":\"31071.6324\"},{\"SEARCHVAL\":\"ORCHARD HOTEL (FRIENDLY " +
    "BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"27469.037\",\"Y\":\"32216.2037\"},{\"SEARCHVAL" +
    "\":\"ORCHARD HOTEL (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreation\",\"X\":\"27469.0369\",\"" +
    "Y\":\"32216.2037\"},{\"SEARCHVAL\":\"ORCHARD HOTEL GALLERIA\",\"CATEGORY\":\"Building\",\"X\"" +
    ":\"27494.5279\",\"Y\":\"32195.9069\"}]}"

我必须使用上述结果将所有名称显示到 Windows Phone 7.1 的列表框中.我该怎么做?

I have to use the above result to display all the name into a listbox in windows phone 7.1. How can I do that ?

我从其他来源看到,他们使用此方法将名称显示到 Windows Phone 7.1 的列表框中:

I have saw from other sources they had this to display names into a list box in windows phone 7.1:

private void Search_Click(object sender, RoutedEventArgs e)
{
    //retrieving the results for the keywords the user input
    searchError.Text = "Loading... Please Wait";
    if (Classes.Global.searched == 1)
    {
        searchVal = new List<string>();
    }
    MobileClinicWebService.MCWebServiceSoapClient obj = new MobileClinicApp.MobileClinicWebService.MCWebServiceSoapClient();
    obj.getSearchCoordsAsync(searchBar.Text.ToString());
    obj.getSearchCoordsCompleted += new EventHandler<MobileClinicWebService.getSearchCoordsCompletedEventArgs>(obj_getSearchCoordsCompleted);
}

void obj_getSearchCoordsCompleted(object sender, MobileClinicWebService.getSearchCoordsCompletedEventArgs e)
{
    //retrieving the results, and displaying it on the phone
    string[] search = null;
    if (!e.Result.ToString().Equals("error"))
    {
        using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(e.Result)))
        {
            while (jsonReader.Read())
            {
                if ((string)jsonReader.Value == "SEARCHVAL")
                {
                    jsonReader.Read();
                    searchVal.Add((string)jsonReader.Value);
                }
                if ((string)jsonReader.Value == "X")
                {
                    jsonReader.Read();
                    posx.Add(Double.Parse(jsonReader.Value.ToString()));
                }
                if ((string)jsonReader.Value == "Y")
                {
                    jsonReader.Read();
                    posy.Add(Double.Parse(jsonReader.Value.ToString()));
                }
            }
        }
        search = new string[searchVal.Count];
        for (int i = 0; i < searchVal.Count; i++)
        {
            search[i] = searchVal[i];
        }
    }
    else
    {
        searchError.Text = "No Results Found";
    }

    if (search != null)
    {
        Classes.Global.searched = 1;
        searchError.Text = "Search Results";
        Results.ItemsSource = search;
    }
}

推荐答案

首先,由于您在 Web 服务源控制 json 响应的结构,您应该修复其结构.

First and foremost, since you control the structure of your json response at the web service source, you should fix its structure.

根据您的数据,搜索结果包含页面结果计数和一组结果,因此我更改了 json 以反映这一点.这意味着可以使用合适的 json 反序列化器进行解析,并使您的代码紧凑整洁.确保您在项目中设置了 Newtonsoft json 库.

From your data, Search result contains a page results count and a set of results, hence I have changed the json to reflect this. This implies that Parsing with a suitable json de-serializer is possible and makes your code compact and tidy. Ensure you have the Newtonsoft json library set up in your project.

C# 类

public class ResultSetPager<T>
{
    public int PageCount { get; set; }
    public IEnumerable<T> SearchResults { get; set; }
}

public class Place
{
    public string SearchVal { get; set; }
    public string Category { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
}

点击事件

        private void LoadJsonData(object sender, RoutedEventArgs e)
    {
        string data = @"{

                         ""PageCount"" : ""1"",
                         ""SearchResults"": [
                            {
                                ""SEARCHVAL"": ""ORCHARD22"",
                                ""CATEGORY"": ""Building"",
                                ""X"": ""29483.4267"",
                                ""Y"": ""31269.938""
                            },
                            {
                                ""SEARCHVAL"": ""ORCHARDBELAIR"",
                                ""CATEGORY"": ""Building"",
                                ""X"": ""27071.2616"",
                                ""Y"": ""31629.2465""
                            }
                        ]
                    }";

        var pagedResults = JsonConvert.DeserializeObject<ResultSetPager<Place>>(data);

        lstPlaces.ItemsSource = pagedResults.SearchResults;
    }

XML

<Grid>
                <StackPanel>
                    <Button Click="LoadJsonData" Content="Test"></Button>

                    <ListBox x:Name="lstPlaces">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                               <StackPanel Margin="0 0 0 15">
                                    <TextBlock Text="{Binding SearchVal}" FontSize="{StaticResource PhoneFontSizeLarge}" />
                                    <TextBlock Text="{Binding Category}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
                               </StackPanel>
                           </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </Grid>

这篇关于从 WCF 服务获取地址名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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