Search code examples
c#web-servicesnestedjsonresult

Json is wrapped with the class name, how to get the raw JSON inner data


I'm building a webservice right now, which I want to consume with an android app. The problem I'm facing is, that the webservice method which should return a string (json) concats it with the method name (Login). I tried to search for this problem, and while I think I'm not the first one with this problem, I found nothing.

My json variable looks like this:

json = "{ \"UserID\": \"" + login.UserID + "\", \"Salutation\": \"" + webUser.Salutation + "\", \"Title\": \"" + webUser.Title + "\", \"Name\": \"" + webUser.Name + "\", \"Lastname\": \"" + webUser.Lastname + "\" } ";

Which gets then returned.

But when I consume it via Postman or my android app, it looks like this:

{ "LoginResult": { \"UserID\": \"" + login.UserID + "\", \"Salutation\": \"" + webUser.Salutation + "\", \"Title\": \"" + webUser.Title + "\", \"Name\": \"" + webUser.Name + "\", \"Lastname\": \"" + webUser.Lastname + "\" }}

This is a behavior that I don't want and I can't find any info on how to stop this.

How can I make my Webservice just return the string that I wrote instead of the methodname concatenated one?

As I can't really wrap my head around why this is happening i just searched if this behavior can be configured somewhere, but I found nothing. also I found nothing searching for this problem.

EDIT: I don't think, this will really change much, but as it was asked, here is the full method a bit obfuscated:

public string Login(string username, string passwort)
        {
            string json = "error";
            try
            {
                var login = Namespace.ViewModels.LoginData.SelectByUserNamePassword(username, passwort);
                Namespace.Models.WebAppUser webUser = new Namespace.Models.WebAppUser();
                lock (webUser)
                {
                    webUser = new Namespace.Controllers.ApplicationController().setWebUserT(Convert.ToInt32(login.UserID), Convert.ToInt32(login.BetriebID), login.BetriebTyp, login.SessionID, login.UniqueID, login.DBName);
                }

                if (webUser != null && webUser.UserID != -1)
                {
                    json = "{ \"UserID\": \"" + login.UserID + "\", \"Salutation\": \"" + webUser.Salutation + "\", \"Title\": \"" + webUser.Title + "\", \"Name\": \"" + webUser.Name + "\", \"Lastname\": \"" + webUser.Lastname + "\" }";
                }
            }
            catch (Exception ex)
            {
                SimpleLogger.Exception(ex);
            }
            return json;
        }

Solution

  • I finally found the answer.

    In my Interface from wich the Serviceclass.svc derives, there is a fomat specified which I overlooked.

    [WebInvoke(Method = "GET", UriTemplate = "Login?username={username}&passwort={passwort}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] string Login(string username, string passwort);

    here I changed the BodyStyle = WebMessageBodyStyle.Wrapped that includes the request into the response into:

    BodyStyle = WebMessageBodyStyle.Bare

    and it doesn't include the name of the method into my json response anymore.