get address name from WCF service

2019-09-06 08:12发布

问题:

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\"}]}"

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 ?

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;
    }
}

回答1:

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

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# Classes

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; }
}

Click event

        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;
    }

Xaml

<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>