2013-03-01 3 views
-1

나는 다음과 같은 코드가 있습니다문제 분석 JSON 객체 응답이

[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} \", \ "성공 \"사실}]} "

내가 만든 개체를 일단 deserialize하면 클래스를 유지합니다.

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

일이 점점에 어떤 도움을 크게

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

client.Headers["Content-type"] = "Application/json"; 
+0

문자열이 RootObject 클래스와 일치하지만 당신은 당신의 코드 샘플에 AddResult로 직렬화 복원되는에 콘텐츠 형식을 변경합니다. – bmavity

+0

감사합니다 bmavity, 내가 RootObject에 deserialize하기 위해 그것을 바꿨을 때 큰 효과를 보았습니다 –

답변

0

변화를 감사 이

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

또는 당신이 당신의 JSON 응답에 AddResult의 목록을 반환하는 당신이

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

을 시도 할 수 있습니다 similary.

그리고 응용 프로그램/JSON

1

시도

+0

답변으로 표시하십시오. 해결책이 있다면 – Sachin