Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析Json部分工作-空子对象_C#_Arrays_Json_.net Core_Deserialization - Fatal编程技术网

C# 解析Json部分工作-空子对象

C# 解析Json部分工作-空子对象,c#,arrays,json,.net-core,deserialization,C#,Arrays,Json,.net Core,Deserialization,我想解析一个json文件。我第一次尝试简化版本只是部分成功 simplifiend json结构如下所示 { "rowCount": 102, "data": [ {"id": "56", "bezeichnung": "Main Center", "strasse": "foostreet"},

我想解析一个json文件。我第一次尝试简化版本只是部分成功

simplifiend json结构如下所示

{
    "rowCount": 102,
    "data": [
        {"id": "56", "bezeichnung": "Main Center", "strasse": "foostreet"},
        {"id": "34", "bezeichnung": "Side Location", "strasse": "5th aveneue"}
    ]
}
public class JsonEnvelope
{
    public int RowCount { get; set; }
    public Location[] Data{ get; set; }
}
public class Location
{
  public string id;
  public string bezeichnung;
  public string strasse;
}
string jsonString = GetJsonFromFile();
var jsonEnvelope = new JsonEnvelope();

var options = new JsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonEnvelope = JsonSerializer.Deserialize<JsonEnvelope>(jsonString, options);
foreach (Location h in jsonEnvelope.Data)
{
    Console.WriteLine(String.Format("{0} in {1}", h.bezeichnung, h.strasse));
}
Console.WriteLine("row count = " + jsonEnvelope.RowCount);
对于外部json
{“rowCount”:102,“data:[]}
我有一个类
jsonedevelope
,如下所示

{
    "rowCount": 102,
    "data": [
        {"id": "56", "bezeichnung": "Main Center", "strasse": "foostreet"},
        {"id": "34", "bezeichnung": "Side Location", "strasse": "5th aveneue"}
    ]
}
public class JsonEnvelope
{
    public int RowCount { get; set; }
    public Location[] Data{ get; set; }
}
public class Location
{
  public string id;
  public string bezeichnung;
  public string strasse;
}
string jsonString = GetJsonFromFile();
var jsonEnvelope = new JsonEnvelope();

var options = new JsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonEnvelope = JsonSerializer.Deserialize<JsonEnvelope>(jsonString, options);
foreach (Location h in jsonEnvelope.Data)
{
    Console.WriteLine(String.Format("{0} in {1}", h.bezeichnung, h.strasse));
}
Console.WriteLine("row count = " + jsonEnvelope.RowCount);
要解析数组数据中的json
“data”:[]
我有
类位置
,如下所示

{
    "rowCount": 102,
    "data": [
        {"id": "56", "bezeichnung": "Main Center", "strasse": "foostreet"},
        {"id": "34", "bezeichnung": "Side Location", "strasse": "5th aveneue"}
    ]
}
public class JsonEnvelope
{
    public int RowCount { get; set; }
    public Location[] Data{ get; set; }
}
public class Location
{
  public string id;
  public string bezeichnung;
  public string strasse;
}
string jsonString = GetJsonFromFile();
var jsonEnvelope = new JsonEnvelope();

var options = new JsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonEnvelope = JsonSerializer.Deserialize<JsonEnvelope>(jsonString, options);
foreach (Location h in jsonEnvelope.Data)
{
    Console.WriteLine(String.Format("{0} in {1}", h.bezeichnung, h.strasse));
}
Console.WriteLine("row count = " + jsonEnvelope.RowCount);
我解析json的代码如下所示

{
    "rowCount": 102,
    "data": [
        {"id": "56", "bezeichnung": "Main Center", "strasse": "foostreet"},
        {"id": "34", "bezeichnung": "Side Location", "strasse": "5th aveneue"}
    ]
}
public class JsonEnvelope
{
    public int RowCount { get; set; }
    public Location[] Data{ get; set; }
}
public class Location
{
  public string id;
  public string bezeichnung;
  public string strasse;
}
string jsonString = GetJsonFromFile();
var jsonEnvelope = new JsonEnvelope();

var options = new JsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonEnvelope = JsonSerializer.Deserialize<JsonEnvelope>(jsonString, options);
foreach (Location h in jsonEnvelope.Data)
{
    Console.WriteLine(String.Format("{0} in {1}", h.bezeichnung, h.strasse));
}
Console.WriteLine("row count = " + jsonEnvelope.RowCount);
string jsonString=GetJsonFromFile();
var jsonedevelope=新的jsonedevelope();
var options=新的JsonSerializerOptions();
options.PropertyNamingPolicy=JsonNamingPolicy.CamelCase;
JSONeDevelope=JsonSerializer.Deserialize(jsonString,选项);
foreach(jsonedevelope.Data中的位置h)
{
WriteLine(String.Format({1},h.bezeichnung,h.strasse中的{0}));
}
Console.WriteLine(“行计数=“+jsonedevelope.RowCount”);
什么在起作用? 行
jsonedevelope.RowCount
起作用。102号被写入控制台

foreach
不工作
h.bezeichnung
h.strasse
的值未写入控制台

问题:
在写我的问题时,我发现了这个问题——我会简单地自我回答。

问题是我在类位置中使用了一个字段,而不是属性。 将类位置更改为此解决了问题

public class Location
{
    public string Id { get; set; }
    public string Bezeichnung { get; set; }
    public string Strasse { get; set; } 
}

问题是我在类位置中使用了字段而不是属性。 将类位置更改为此解决了问题

public class Location
{
    public string Id { get; set; }
    public string Bezeichnung { get; set; }
    public string Strasse { get; set; } 
}
请注意:

  • .NET 5或
  • System.Text.Json包版本5添加到Core3.1项目中,您可以使用
    IncludeFields
    选项:
var serializeOptions=新的JsonSerializerOptions
{
includefelds=true,
};
var o=JsonSerializer.Deserialize(json,serializeOptions);
请注意:

  • .NET 5或
  • System.Text.Json包版本5添加到Core3.1项目中,您可以使用
    IncludeFields
    选项:
var serializeOptions=新的JsonSerializerOptions
{
includefelds=true,
};
var o=JsonSerializer.Deserialize(json,serializeOptions);

谢谢你指出这一点。提示“仅对具有私有或受保护可访问性的变量使用字段。您的类向客户端代码公开的数据应通过方法、属性和索引器提供。”感谢您指出这一点。提示“仅对具有私有或受保护可访问性的变量使用字段。应通过方法、属性和索引器提供类向客户端代码公开的数据。”