反序列化对象返回空对象

3

我试图从项目文件夹中读取文件,并将其作为json返回内容。
这是我的代码:

string allText = System.IO.File.ReadAllText(jsonPath);
object jsonObject = JsonConvert.DeserializeObject(allText);
return Ok(jsonObject);

这是文件内容:
[{"grouping":"IS3","count":327.0,"population":12.0,"weight":31.0},
{"grouping":"ISO","count":31.0,"population":31.0,"weight":27.0}]

This is the response from postman:
[
    [
        [
            []
        ],
        [
            []
        ]
    ]
]

If I put a break point I can see that the jsonObject is filled correctly.

I forgot to say I made the file with another endpoint reading from a file excel. The fields are not always the same, I made the object dinamicaly so I can't use a class.

I'd like to return a json as content type, not a text.



你如何使用PostMan? - Dilshod K
你能给我们展示一下Postman API调用的快照吗?这可能会给我们一些想法。 - Prasad Telkikar
1
jsonObject.GetType()是什么? - mjwills
你收到的JSON格式与你正在反序列化的完全不同。 - Jamshaid K.
请提供更多的代码,这三行代码运行良好并给出有效的输出。 - Kartik Maheshwari
显示剩余3条评论
2个回答

3
您使用JSON.Net进行反序列化,但没有提供类型。这意味着它将反序列化为JArrayJObject类型。然后您正在使用ASP.NET Core的默认序列化程序,它是System.Text.Json,该程序不知道如何处理JSON.Net类型,这就是为什么会出现所有这些嵌套数组的原因。坚持使用一种序列化方法。您有多种选择,以下是其中几种:
  1. Use a proper class to deserialise and stick with System.Text.Json. This would be my preferred option.

    // Add these if missing:
    //using System.Text.Json;
    //using System.Text.Json.Serialization;
    
    public class MyThing
    {
        [JsonPropertyName("grouping")]
        public string Grouping { get; set; }
        [JsonPropertyName("count")]
        public double Count { get; set; }
        [JsonPropertyName("population")]
        public double Population { get; set; }
        [JsonPropertyName("weight")]
        public double Weight { get; set; }
    }
    

    And deserialise like this:

    var jsonObject = JsonSerializer.Deserialize<List<MyThing>>(allText);
    return Ok(jsonObject);
    

    Note: If you cannot create a class because the JSON is dynamic, you could fall back to using JsonSerializer.Deserialize<object>(allText)

  2. Send the JSON string back to the client manually

    return new JsonResult(jsonObject.ToString());
    

我无法创建一个类,因为每次读取文件时都会更改。我读取Excel文件,动态地创建对象,然后将其写入文件。 - Federico Alberti
检查我添加关于使用 object 的注释的那一行。 - DavidG
我尝试了,但可悲的是没有任何改变。 - Federico Alberti
1
@DavidG 这样做会使终端返回一个字符串,text/plain 格式而不是 JSON(application/json)格式。这种行为可能会给客户端带来问题。 - Giox
我该如何返回JSON(application/json)? - Federico Alberti
显示剩余2条评论

0

你需要对类型进行建模并反序列化JSON。不需要添加任何属性注释。这应该可以工作。

public class MyObject
{

    public string Grouping { get; set; }

    public double Count { get; set; }

    public double Population { get; set; }

    public double Weight { get; set; }
}

然后你可以,

// read file into a string and deserialize JSON to a type
MyObject object1 = JsonConvert.DeserializeObject<List<MyObject>>(File.ReadAllText(@"c:\alltext.json"));
return Ok(object1);

问题在于现在使用了两个不同的JSON库。 - DavidG
抱歉,我没听懂。这样怎么样?我刚刚运行了代码,看起来正常。只是导入了JSONConvert库。 - Sohan
return Ok(object1); 在 ASP.NET MVC Core 中使用的是默认序列化器 System.Text.Json,而不是 Newtonsoft。请查看我的答案。 - DavidG
每次我使用另一个端点读取Excel文件时,MyObject都会发生变化。我没有静态模型来表示MyObject,而是动态生成的。 - Federico Alberti
@FedericoAlberti 我在我的答案中添加了一行,以展示当您的JSON是动态的时该怎么做。 - DavidG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接