2012-07-12 17 views
7

Sabah,WebService'den nasıl JSON dönülür

Web servisimden bir ileti göndermem gerekiyor. Aşağıda kodumun bir örneği var ve bir string döndürüyorum.

[web method] 
public string CheckFeedSubmission() 
    { 
     string responseText = ""; 
     try 
     { 
      //Stuff goes here 
      responseText = "It Worked!" 
     } 
     catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; } 
     return responseText ; 
    } 

Şu anda şu yanıtı almak ...

<string xmlns="http://tempuri.org/"/> 
Ben ideal ben bu fikri aldıktan sonra eminim

{"success" : true, "message" : "***Message Here***"} 

gibi bir şey dönmek istiyorum

i Gerekirse diğer öğeleri iade edebilecektir. Sadece bu üssü çalışmam gerek.

Tüm yardım çok önceden :)

GÜNCELLEME içinde sayesinde takdir: Sadece bu buldum ...

return "{Message:'hello world'}" 

i

responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}" 
+0

olası yinelenen [web hizmeti json dönmelidir] (http://stackoverflow.com/questions/8205081/web -service-should-return-json) –

cevap

10

Kullanım gibi bir şeye ihtiyacım misiniz:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format. 
public string CheckFeedSubmission() 
    { 
     string responseText = ""; 
     try 
     { 
      //Stuff goes here 
      responseText = "It Worked!" 
     } 
     catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; } 
     return responseText ; 
    } 

gibi olacak dönen sonucu:

<string xmlns="http://tempuri.org/"/> 
{"success" : true, "message" : "***Message Here***"} 
</string> 
+2

Teşekkürler, ama yukarıdaki gibi hala XML dize geri aldım – thatuxguy

+0

Aslında Json XML içinde döndürür. Aramak istediğiniz şeyi geri aramak istediğinizi belirtmeniz gerekir. Neden biçiminde dönüyor? http://haacked.com/archive/2009/06/25/json-hijacking.aspx –

+0

"İşe yaradı!" cevap mesajı – thatuxguy

2

sizin WebMethod için özelliğini kullanın

arayan WebMethod kullanmak uygulama/json yaptığı ContentType'ı ayarlamış olacaktır

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

+3

çalışmıyor, hala sadece bir dize geri alın - Tamam çalıştı thatuxguy

0

Bunu deneyin:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format. 
public bool addUser(UserModel um) 
    { 
     bool result = false; 
     result = Conversion.intToBool(SplashAwardsDB.executeNonQuery(
      "INSERT INTO dbo.User (" 
      + "userName, password, firstName, lastName, address, contactNo, birthDate, familyID, familyRole, x, y) " 
      + " VALUES (" 
      + "'" + um.userName + "', " 
      + "'" + um.password + "', " 
      + "'" + um.firstName + "', " 
      + "'" + um.lastName + "', " 
      + "'" + um.address + "', " 
      + "'" + um.contactNo + "', " 
      + "'" + um.birthDate + "', " 
      + "'" + um.familyID + "', " 
      + "'" + um.familyRole + "', " 
      + "'" + um.x + "', " 
      + "'" + um.y + "')" 
      )); 
     return result; 
    } 
İlgili konular