【问题标题】:Calling and get result text from service从服务调用并获取结果文本
【发布时间】:2012-03-05 07:54:04
【问题描述】:

我有服务 url:please check it,它是返回 json 字符串。

我不知道如何使用C#调用这个url

那么。通过链接,您可以在 json 数据中看到"href"。它有.png 网址。我想将该图像保存到我的本地磁盘。

问题

  1. 如何从这个service获取和读取json数据

  2. 如何将图片从url保存到本地磁盘

【问题讨论】:

  • @f0x 这是一个类,我已经为ServiceFacade提供了调用它。

标签: c# json


【解决方案1】:

我想您需要来自服务器而不是来自客户端的服务请求;如果是这样一个简单的方法将是

string json = null;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");
HttpWebResponse response = null;

try
{
    response = (HttpWebResponse)request.GetResponse();
    var responseStream = response.GetResponseStream();

    if ((responseStream != null) && responseStream.CanRead)
    {
        using (var reader = new System.IO.StreamReader(responseStream))
        {
            json = reader.ReadToEnd();
        }
    }
}
finally
{
    if (response != null)
    {
        response.Close();
    }
}

同样的方法可以得到图片。

对于 Json,我推荐 JSON.NET

【讨论】:

    【解决方案2】:

    使用数据协定和DataContractJsonSerializer

    需要参考:

    using System.Runtime.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Net;
    

    数据合同

    [DataContract]
    internal class GISData
    {
        [DataMember]
        public string href;
    
        [DataMember]
        public int width;
    
        [DataMember]
        public int height;
    
        [DataMember]
        public GISDataExtent extent;
    
        [DataMember]
        public string scale;
    }
    
    [DataContract]
    internal class GISDataExtent
    {
        [DataMember]
        public string xmin;
    
        [DataMember]
        public string ymin;
    
        [DataMember]
        public string xmax;
    
        [DataMember]
        public string ymax;
    
        [DataMember]
        public GISDataExtentSpatialReference spatialReference;
    
    }
    
    [DataContract]
    internal class GISDataExtentSpatialReference
    {
        [DataMember]
        public string wkid;
    }
    

    将它们放在一起并下载文件:

    从您的 URL 中提取 JSON 字符串 - 反序列化对象并下载您的图像。

    WebClient webClient;
    webClient = new WebClient();
    string json = webClient.DownloadString(@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");
    
    MemoryStream stream = new MemoryStream((Encoding.UTF8.GetBytes(json)));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(GISData));
    stream.Position = 0;
    GISData data = (GISData)ser.ReadObject(stream);
    stream.Close();
    
    webClient = new WebClient();
    webClient.DownloadFile(data.href, "C:/" + data.href.Substring(data.href.LastIndexOf("/") + 1)); //Save only the filename and not the entire URL as a name.
    

    【讨论】:

    • 你可以做的更简单:GISData data = (GISData)ser.ReadObject(@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/export?bbox=9153621.1267%2C-644273.9207%2C13435472.9966%2C3637577.9492&bboxSR=&layers=&layerdefs=&size=800%2C600&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&f=pjson");
    • 你也可以省略 DataXXX 属性:)
    • @the_joric,谢谢; ReadObject 的最佳重载似乎是在寻找流?
    【解决方案3】:
    1. 添加Json.NET包(参考How to install JSON.NET using NuGet?
    2. 使用以下代码:

      using Newtonsoft.Json;
      
      ......
      
      string url = "...";
      
      var webClient = new WebClient();
      var s = webClient.DownloadString(url);
      dynamic d = JsonConvert.DeserializeObject(s);
      var href = ((string) d.href);
      
      webClient.DownloadFile(href, @"d:\file.png");
      

    请注意,您的 png 文件不存在 :)

    如果你愿意,你可以创建你的 POCO 类。但如果你只需要下载一个文件——那可能就不需要了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多