Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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# 来自datatable和IList的嵌套JSON_C#_Json_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 来自datatable和IList的嵌套JSON

C# 来自datatable和IList的嵌套JSON,c#,json,asp.net-mvc,asp.net-mvc-4,C#,Json,Asp.net Mvc,Asp.net Mvc 4,我有两个变量 Datatable列表对象 它产生的JSON如下所示 "listofObject" : [ { "Obj1": "Some String Value", "Obj2": "Some Date Value", "Obj3": "Some Int Value", "Obj4": "Some Date Value", .... } ] 还有另一个物体 IList ObjectInfo 它产生的JSON如下所示 "ObjectInfo" : [ {

我有两个变量

Datatable列表对象

它产生的JSON如下所示

"listofObject" : [
{
   "Obj1": "Some String Value",
   "Obj2": "Some Date Value",
   "Obj3": "Some Int Value",
   "Obj4": "Some Date Value",
....
}
] 
还有另一个物体

IList ObjectInfo

它产生的JSON如下所示

"ObjectInfo" : [
  {
     "name" : "Obj1",
     "Style" : "Style Name",
     "Data Type" : "String"
  },
  {
     "name" : "Obj2",
     "Style" : "Style Name",
     "Data Type" : "Date"
  },
  {
     "name" : "Obj3",
     "Style" : "Style Name",
     "Data Type" : "Int"
  },
.....
]
我如何将它们组合成下面这样的JSON结构

"finalStructure" :[
  "Obj1": {
         "Style" : "Style Name",
         "Data Type" : "String"
          },
  "Obj2": {
         "Style" : "Style Name",
         "Data Type" : "Date"
          },
  "Obj3": {
         "Style" : "Style Name",
         "Data Type" : "Int"
          },
....
]

从数据表中,您有一个
IEnumerable
,因此您要做的是首先创建一个查找,然后您可以安全地投影结果:

var items = listOfObjects.FromJson<IEnumerable<IDictionary<string, string>>>();

var info = objectInfo.FromJson<IEnumerable<IDictionary<string, string>>>()
    .ToLookup(it => it.Single(k => k.Key == "name").Value, it => it.Where(k => k.Key != "name"));

var restructured = items
    .SelectMany(it => it.Keys)
    .GroupBy(it => it)
    .Select(it => new
    {
        Key = it.Key,
        Value = info[it.Key].SelectMany(fo => fo).ToDictionary(fo => fo.Key, fo => fo.Value)
    })
    .ToDictionary(it => it.Key, it => it.Value);

// extension method with NewtonSoft.JSON.net
public static T FromJson<T>(this string json)
{
    var serializer = new JsonSerializer();
    using (var sr = new StringReader(json))
    using (var jr = new JsonTextReader(sr))
    {
        var result = serializer.Deserialize<T>(jr);
        return result;
    }
}
var items=listOfObjects.FromJson();
var info=objectInfo.FromJson()
.ToLookup(it=>it.Single(k=>k.Key==“name”).Value,it=>it.Where(k=>k.Key!=“name”);
var重组=项目
.SelectMany(it=>it.Keys)
.GroupBy(it=>it)
.选择(it=>new
{
Key=it.Key,
Value=info[it.Key].SelectMany(fo=>fo.ToDictionary(fo=>fo.Key,fo=>fo.Value)
})
.ToDictionary(it=>it.Key,it=>it.Value);
//使用NewtonSoft.JSON.net的扩展方法
公共静态T FromJson(此字符串为json)
{
var serializer=new JsonSerializer();
使用(var sr=newstringreader(json))
使用(var jr=新的JsonTextReader(sr))
{
var result=serializer.Deserialize(jr);
返回结果;
}
}

您可以创建一个类,模拟包含两个实例数据的预期结构,不幸的是,目前您没有展示您当前实现这一目标的尝试。在我看来,您根本没有将它们组合在一起;最终的JSON结构只包含
ObjectInfo
列表中的数据。
DataTable
中的值应该在那里的某个地方吗?另外,您要求的最终结构是无效的JSON;键值对不能直接出现在数组内部。你的意思是让那些方括号成为花括号吗?@BrianRogers是的,我只有在它们是花括号的情况下才能工作。(不使用任何类)