Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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#(NewtonSoft)编辑JSON,因为我收到一个错误_C#_Json_Json.net - Fatal编程技术网

无法使用C#(NewtonSoft)编辑JSON,因为我收到一个错误

无法使用C#(NewtonSoft)编辑JSON,因为我收到一个错误,c#,json,json.net,C#,Json,Json.net,我试图从一个JSON文件中更改JSON,以便运行它以获得JSON响应,我在这篇文章中找到了代码:有人能修复这个问题吗 错误: Newtonsoft.Json.dll中发生类型为“System.ArgumentException”的未处理异常 其他信息: 设置具有无效键值的JArray值:“过滤器”。应为Int32数组索引 JSON: 代码: (JSON用于与Web服务对话)我认为这应该是: jsonObj[0]["filter"] = "id = 20"; 因为JSON是一个数组。因此,0是数

我试图从一个JSON文件中更改JSON,以便运行它以获得JSON响应,我在这篇文章中找到了代码:有人能修复这个问题吗

错误:

Newtonsoft.Json.dll中发生类型为“System.ArgumentException”的未处理异常

其他信息: 设置具有无效键值的JArray值:“过滤器”。应为Int32数组索引

JSON:

代码:


(JSON用于与Web服务对话)

我认为这应该是:

jsonObj[0]["filter"] = "id = 20";

因为JSON是一个数组。因此,0是数组中的第一个对象。

您有一些选项,但如果将其反序列化为动态对象,则可以尝试以下操作

var json = File.ReadAllText("file.json");
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

jsonObj[0].filter = "id = 20"; //<--CALLING PROPERTY DIRECTLY via DynamicObject

var output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("file.json", output);
用这个

var json = File.ReadAllText("file.json");
//Deserializing Object to Model Array
var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Model[]>(json);

jsonObj[0].filter = "id = 20"; //<--CALLING PROPERTY DIRECTLY

var output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("file.json", output);
var json=File.ReadAllText(“File.json”);
//将对象反序列化为模型数组
var jsonObj=Newtonsoft.Json.JsonConvert.DeserializeObject(Json);

jsonObj[0].filter=“id=20”//<代码>jsonObj[0][“过滤器”]=“id=20”您的JSON是一个数组,所以您必须指定一个索引,即使它只有1个项目长。
var json = File.ReadAllText("file.json");
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

jsonObj[0].filter = "id = 20"; //<--CALLING PROPERTY DIRECTLY via DynamicObject

var output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("file.json", output);
public class Model
{
    public string tablename { get; set; }
    public string columns { get; set; }
    public string filter { get; set; }
}
var json = File.ReadAllText("file.json");
//Deserializing Object to Model Array
var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Model[]>(json);

jsonObj[0].filter = "id = 20"; //<--CALLING PROPERTY DIRECTLY

var output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("file.json", output);