Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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#_Json_Asp.net Mvc_Upload_Redactor - Fatal编程技术网

是否可以使用c#类创建此JSON格式?

是否可以使用c#类创建此JSON格式?,c#,json,asp.net-mvc,upload,redactor,C#,Json,Asp.net Mvc,Upload,Redactor,我正在努力解决中的多个图像上传问题。具体来说,在上传和保存图像后创建返回JSON 我已经设法使用StringBuilder实现了这一点,但是如果可能的话,我想在一个正确类型的类中实现,使用返回Json(redactorResult,JsonRequestBehavior.AllowGet) 所需格式 从中,我可以看到我需要的格式是: { "file-0": { "url":"/tmp/images/b047d39707366f0b3db9985f97a56267.jpg

我正在努力解决中的多个图像上传问题。具体来说,在上传和保存图像后创建返回JSON

我已经设法使用
StringBuilder
实现了这一点,但是如果可能的话,我想在一个正确类型的类中实现,使用
返回Json(redactorResult,JsonRequestBehavior.AllowGet)


所需格式

从中,我可以看到我需要的格式是:

{
    "file-0": {
        "url":"/tmp/images/b047d39707366f0b3db9985f97a56267.jpg",
    "   id":"70e8f28f4ce4a338e12693e478185961"
    },
    "file-1":{
        "url":"/tmp/images/b047d39707366f0b3db9985f97a56267.jpg",
        "id":"456d9b652c72a262307e0108f91528a7"
    }
}
C#Class

但要从c#类创建JSON,我想我需要这样的东西:

public class RedactorFileResult
{
     public string url {get;set;}
     public string id {get;set;}
}

public class RedactorResult
{
    public RedactorFileResult file-1 {get;set;}
    public RedactorFileResult file-2 {get;set;}
    // .. another property for each file...
}
。。。这在最坏的情况下似乎是不可能的(永远不知道会上传多少图像),在最好的情况下也有点不切实际

问题

我的做法正确吗?有没有我不知道的方法


还是在这种情况下我最好还是坚持使用字符串生成器?

为单个项目定义一个类:

public class File
{
    public string Url { get; set; }
    public string Id { get; set; }
}
然后按如下方式创建文件:

var files = new List<File>();
files.Add(new File{Url = "tmp/abc.jpg", Id = "42"});
files.Add(new File {Url = "tmp/cba.jpg", Id = "24"});
var redactorResult = new Dictionary<string, Item>();
redactorResult["file-1"] = new Item(){ Url = "...", Id = "..." };
redactorResult["file-2"] = new Item(){ Url = "...", Id = "..." }; 
在我的示例中,结果是:

{
  "file-0":{
    "Url":"tmp/abc.jpg",
    "Id":"42"
  },
  "file-1":{
    "Url":"tmp/cba.jpg",
    "Id":"24"
  }
}        
定义项目:

class Item 
{
    public string Url { get; set; }
    public string Id { get; set; }  
}
然后应该使用字典而不是RedactorResult类,如下所示:

var files = new List<File>();
files.Add(new File{Url = "tmp/abc.jpg", Id = "42"});
files.Add(new File {Url = "tmp/cba.jpg", Id = "24"});
var redactorResult = new Dictionary<string, Item>();
redactorResult["file-1"] = new Item(){ Url = "...", Id = "..." };
redactorResult["file-2"] = new Item(){ Url = "...", Id = "..." }; 
var-redactorResult=new Dictionary();
redactorResult[“文件-1”]=新项(){Url=“…”,Id=“…”};
redactorResult[“文件-2”]=新项(){Url=“…”,Id=“…”};
如果您更喜欢RedactorResult类,可以扩展字典:

class RedactorResult : Dictionary<string, Item>
{
    private int count = 0;

    public void Add(Item item) 
    {
        this[$"file-{count}"] = item;
        count++;
    }
}
类编校结果:字典
{
私有整数计数=0;
公共作废添加(项目)
{
此[$”文件-{count}]=项;
计数++;
}
}

可能会使用字典,该链接不会显示任何关于json格式的内容。你能再检查一下吗?我希望你是错的,创建顺序对象而不是数组是非常糟糕的。字典应该可以。@Crowcoder-我觉得很奇怪,但我不是专家。我刚刚尝试在演示页面上传多个图像,并查看了dev tools中返回的
json
。非常感谢,这正是我需要了解的信息,并让它正常工作。谢谢Matteus-感谢你的回答,Aleks刚刚把你推到了帖子上