JObject 返回空数据

huangapple go评论108阅读模式
英文:

JObject returning empty data

问题

我正在尝试在我的API中返回详细的反馈信息,所以创建了一个类:

public class Notify
{
    public class Root
    {
        public string Error { get; set; }
        public Details Details { get; set; }
    }

    public class Details
    {
        public string FeedbackText { get; set; }
        // 其他属性...
    }
}

在我的类中,我使用以下方式填充对象:

public JObject NotifyUser()
{
    var r = new Notify.Root
    {
        Error = "Some error",
        Details = new Notify.Details
        {
            FeedbackText = "Something to do"
        }
    };

    return JObject.Parse(DoSomethingWithJson(r));
}

DoSomethingWithJson 是一个返回字符串的方法,将对象传递进去以修改数据。

在我的API中,我有以下代码:

var currentValue = NotifyUser(); // 我能看到这个变量中的 JSON 和值,但下一行只显示 JSON 字段而没有任何数据。
return StatusCode(HttpStatusCode.BadRequest, currentValue);

但它只返回 JSON 字段而没有任何数据。我将其转换为字符串,一切都显示出来,包括分配给每个字段的文本,但响应被列为文本而不是 JSON。

我尝试过将代码转换为 JsonConvert.SerializeObject,但如果它是字符串格式,则显示数据,否则为空属性(我希望响应被编码为 JSON)。

我不确定为什么会这样做?我漏掉了什么?我搜索过,但大多数都是针对旧版本的,所以我觉得我在这里做错了什么。

英文:

Im trying to return detailed feedback in my API so created a class:

public class Notify
{
   public class Root
   {
     Public string Error {get; set;}
     Public Details Details {get; set;}
   }

  public class Details
   {
     public string FeedbackText {get; set;}
     ....
   }
}

In my class i populate the object with

public JObject NotifyUser()
{
     var r = new Notify.Root 
     { 
     Error = "Some error",
     Details = new Notify.Details
           { 
             FeedbackText = "Something to do"
            }
     };

     return JObject.Parse(DoSomethingWithJson(r));
}

DoSomethingWithJson is a string method and passing in the object to modify the data.

In my API i have

var currentValue = NotifyUser(); // I see the Json and values in this variable but next line doesnt display the values only the Json fields
return StatusCode(HttpStatusCode.BadRequest, currentValue);

but it just returns the JSON fields without any data against any field.
I convert it to a string and everything appears including the text assigned to each field but the response is listed as text and not JSON.

Ive tried converting the code to JsonConvert.SerialiseObject but if its in a string format it shows the data otherwise its empty properties (i would like the response to be encoded as json).

Im not sure why its doing this? What have i missed? I did search around but most were targeting old versions so i feel im doing something wrong here.

答案1

得分: 1

你的代码甚至无法编译。你只能用Parse来处理JSON字符串。但你有一个C#对象。在提问之前,你应该尝试编译你的代码。最后一行应该是

return JObject.FromObject(r);

但可能你不需要将其转换为JObject,因为API操作会自动将其序列化为JSON字符串。无论是JSON对象还是C#对象都没有关系。

英文:

Your code can not be even compilled. You can use Parse only for a json string. But you have a c# object. You should try to compile your code before asking the question. The last line should be

return JObject.FromObject(r);

But probably you don't need to convert it to JObject since APi action will automatically serialize it to a json string. It doesnt matter if it is a json object or c# object.

huangapple
  • 本文由 发表于 2023年6月8日 23:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定