-1

次のコードがあります。

[WebMethod]
public bool AddUser(long warnId, double longitude, double latitude)
{
    try
    {
        string jsonFeature = string.Empty;
        jsonFeature += "[{'geometry': {'x': " + longitude + ",'y': " + latitude +
                       ",'spatialReference': {'wkid': 4326}},'attributes': {'UID': '";
        jsonFeature += warnId + "','Latitude': '" + latitude + "','Longitude': '" + longitude + "'}}]";

        string reqURL = System.Configuration.ConfigurationManager.AppSettings["WARNURL"] + "addFeatures";

        using (System.Net.WebClient client = new System.Net.WebClient())
        {
            client.Headers["Content-type"] = "application/x-www-form-urlencoded";
            client.Encoding = System.Text.Encoding.UTF8;
            var collection = new System.Collections.Specialized.NameValueCollection();
            collection.Add("f", "json");
            collection.Add("features", jsonFeature);
            byte[] response = client.UploadValues(reqURL, "POST", collection);
            MemoryStream stream = new MemoryStream(response);
            StreamReader reader = new StreamReader(stream);
            string aRespStr = reader.ReadToEnd();

            JavaScriptSerializer jss = new JavaScriptSerializer();
            AddResult addResult = jss.Deserialize<AddResult>(aRespStr);


            return aRespStr.Contains("true") && aRespStr.Contains("success");
        }
    }
    catch(Exception e)
    {
        string message = e.Message;
        return false;
    }
}

実行すると、文字列 aRespStr は次のようになります。

"{\"addResults\":[{\"objectId\":28,\"globalId\":\"{740490C6-77EE-4AC0-9561-5EBAACE3A0A7} \",\"成功\":true}]} "

オブジェクトを逆シリアル化した後、オブジェクトを保持するために次のクラスを作成しました。

    public class Error
{
    public int code { get; set; }
    public string description { get; set; }
}

public class AddResult
{
    public int objectId { get; set; }
    public object globalId { get; set; }
    public bool success { get; set; }
    public Error error { get; set; }
}

public class RootObject
{
    public List<AddResult> addResults { get; set; }
}

しかし、コードを実行すると、addResult オブジェクトには json オブジェクト値ではなく、デフォルトのオブジェクト値が含まれます。

この特定の応答の API は、http: //help.arcgis.com/en/arcgisserver/10.0/apis/rest/fsadd.htmlにあります。

これを機能させるための助けがあれば大歓迎です

4

2 に答える 2

1

これを試して

RootObject addResults=jss.Deserialize<RootObject>(aRespStr);

または同様に、これを試すことができます

List<AddResult> addResults = jss.Deserialize<List<AddResult>>(aRespStr);

json Response で AddResult のリストを返しているためです。

コンテンツ タイプを application/json に変更します。

于 2013-03-01T20:05:43.350 に答える
0

変化する

client.Headers["Content-type"] = "application/x-www-form-urlencoded";

client.Headers["Content-type"] = "Application/json";
于 2013-03-01T19:54:27.827 に答える