格式化JSON对象数据 [英] Formating JSON object data

查看:115
本文介绍了格式化JSON对象数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何修改控制器中的JSON对象,以便按照指定的顺序显示数据,如第二张图片所示。







Quote:

预期的Json数据





 {
表:[
{
中央CDC:[Sengkang West GRC,def ]
},
{
东北CDC:[Pasir Ris - Punggol GRC,Tampines GRC]
},







Quote:

代码为控制器



 使用系统; 
使用 System.Collections.Generic;
使用 System.Data;
使用 System.Linq;
使用 System.Net;
使用 System.Net.Http;
使用 System.Threading.Tasks;
使用 System.Web.Http;
使用 System.Web.Http.Description;
使用 System.Web.Http.Results;
使用 System.Web.Mvc;

命名空间 Prototype3.Controllers
{
public class GAROController:ApiController
{
Database_Access_Data.db dblayer = new Database_Access_Data.db ();


[System.Web.Http.HttpGet]
[System.Web.Http.Route( < span class =code-string> api / GARO / GetGAROList)]
public DataSet CheckLockedOut()
{
DataSet ds = dblayer.GetGAROList();
return ds;
}

}
}



Quote:

以及从数据库调用存储过程的代码:



  public  DataSet GetGAROList()
{
SqlCommand com = new SqlCommand( GetCDC,con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}



Quote:

存储过程检索来自数据库的数据





  / *  < span class =code-comment> *****对象:StoredProcedure [dbo]。[GetCDC]脚本日期:
4/12/2018 4:33:23 PM ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]。[GetCDC]
AS
BEGIN
SELECT E.Name AS ED_N ame,displayname = STUFF(( SELECT DISTINCT ' ,' +
D.Name
FROM ElectoralDivision D
WHERE E.ID = D.GAROID
FOR XML PATH(' )), 1 2 ' '
From GARO E
GROUP BY E.ID, E.Name
END





< b>我尝试了什么:



尝试研究格式化JSON数据的任何解决方案但无济于事。

解决方案

我似乎无法找到您所指的图片,但是,如果您尝试将JSON对象格式化为所需格式,我建议您查看inti JSON.Net 如何使用C#在.NET中获取格式化JSON? - 堆栈溢出 [ ^ ]


Anyone knows how can i modify the JSON Object in the controller such that it would display the data in a specified order which is shown on the 2nd picture.



Quote:

Intended Json Data



{
    "Table": [
        {
            "Central CDC":["Sengkang West GRC", "def"]
        },
        {
           "North-East CDC": ["Pasir Ris - Punggol GRC", "Tampines GRC"]
        },




Quote:

Code for the Controller


 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Threading.Tasks;
 using System.Web.Http;
 using System.Web.Http.Description;
 using System.Web.Http.Results;
 using System.Web.Mvc;

 namespace Prototype3.Controllers
 {
 public class GAROController : ApiController
{
    Database_Access_Data.db dblayer = new Database_Access_Data.db();


    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("api/GARO/GetGAROList")]
    public DataSet CheckLockedOut()
    {
        DataSet ds = dblayer.GetGAROList();
        return ds;
    }

}
 }


Quote:

And code which calls the stored procedure from the database:


public DataSet GetGAROList()
{
    SqlCommand com = new SqlCommand("GetCDC", con);
    com.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds;
}


Quote:

Stored Procedure to retrieve the data from the database



/****** Object:  StoredProcedure [dbo].[GetCDC]    Script Date:
4/12/2018 4:33:23 PM ******/
SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO
 ALTER PROCEDURE [dbo].[GetCDC]
 AS
  BEGIN
  SELECT E.Name AS ED_Name, displayname = STUFF((SELECT DISTINCT ', ' +
   D.Name
   FROM ElectoralDivision D
   WHERE E.ID = D.GAROID
  FOR XML PATH('')), 1, 2, '')
  From GARO E
  GROUP BY E.ID,E.Name
  END



What I have tried:

Tried to researching for any solution in formatting the JSON Data but to no avail.

解决方案

I can't seem to find the picture that you were referring to, however, if you are trying to format your JSON object into a desired format, I would suggest you to look inti JSON.Net: How do I get formatted JSON in .NET using C#? - Stack Overflow[^]


这篇关于格式化JSON对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆