简体   繁体   English

如何在ASP.NET中使用JSON数据格式JavaScript Serializer

[英]How to JSON Data Format JavaScript Serializer in ASP.NET

I am writing an ASP.NET web service. 我正在编写一个ASP.NET Web服务。 I made the connection to SQL Server. 我建立了与SQL Server的连接。 The web service is using a JavaScript serializer. Web服务使用JavaScript序列化程序。 My JSON format: 我的JSON格式:

[{"ID":1,"TCKN":"19","Adi":"zzz","Soyadi":"aa"},
{"ID":2,"TCKN":"99","Adi":"user","Soyadi":"user"}]

But I want this JSON format: 但我想要这种JSON格式:

"users": [
    {
            "ID": "1",
            "TCKN": "19",
            "Adi": "zzz",
            "Soyadi": "aa"
    },
    {
            "ID": "2",
            "TCKN": "99",
            "Adi": "user",
            "Soyadi": "user"
    },]

WebServices.cs WebServices.cs

  [WebMethod]
public string getTumKullanici()
{
    JavaScriptSerializer jss = new JavaScriptSerializer();
    var json = "";
    var tumKullanicilar = from result in mydb.Kullanicilars
                          select result;

    json = jss.Serialize(tumKullanicilar);

    return json;
}

Whenever you're dealing with JSON data that you need to serialize into C#, go to Json2CSharp 每当您处理需要序列化为C#的JSON数据时,请转到Json2CSharp

Using that site I've got your C# data model: 使用该网站我有你的C#数据模型:

public class RootObject
{
    public int ID { get; set; }
    public string TCKN { get; set; }
    public string Adi { get; set; }
    public string Soyadi { get; set; }
}

Next you're going to need JSON.NET 接下来,您将需要JSON.NET

You can get that by opening the nuget package manager and searching Newtonsoft.Json 您可以通过打开nuget包管理器并搜索Newtonsoft.Json来实现

Or use the Package manager console 或者使用包管理器控制台

PM> Install-Package Newtonsoft.Json 

Next you're going to need to deserialize that JSON.. 接下来,您将需要反序列化该JSON ..

string json = @"{""ID"":1,""TCKN"":""19"",""Adi"":""zzz"",""Soyadi"":""aa""},{""ID"":2,""TCKN"":""99"",""Adi"":""user"",""Soyadi"":""user""}]";

RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

And if you never need to serialize the C# object back into JSON... You can do this. 如果你永远不需要将C#对象序列化为JSON ......你可以这样做。

string backToJson = JsonConvert.SerializeObject(root);

EDIT 编辑

You can achieve nesting the under users by wrapping your original object with another class: 您可以通过使用另一个类包装原始对象来实现对用户的嵌套:

public class Users
{
    public int ID { get; set; }
    public string TCKN { get; set; }
    public string Adi { get; set; }
    public string Soyadi { get; set; }
}

public class Root
{
    public List<User> Users {get;set;}
}

var root = new Root { Users = mydb.Kullanicilars.ToList() };

JsonConvert.SerializeObject(root);

My understanding is that the way the JSON is displayed lacks of new line chars etc - that's completely normal and desired, as extra characters are useless for services. 我的理解是,显示JSON的方式缺少新的行字符等 - 这是完全正常和期望的,因为额外的字符对于服务是无用的。 To make the JSON more readable to humans, you could use tools that enable formatting, as fe addon to Notepad++ or some webapp. 为了使JSON对人类更具可读性,您可以使用启用格式化的工具,例如Notepad ++或某些webapp。 But for general use as data in computing I would stick to raw format without the tabulators and new lines. 但是作为计算中的数据的一般用途,我会坚持使用原始格式而不使用制表符和新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM