简体   繁体   English

在不带JSON.Net的UWP中创建JSON

[英]Create json in UWP without JSON.Net

I am trying to create a json formatted string using c# in UWP without JSON.Net, but I am just not understanding how to get there. 我正在尝试在不带JSON.Net的UWP中使用c#创建json格式的字符串,但我只是不了解如何到达那里。 Let's say I wanted to create the following json dynamically: 假设我想动态创建以下json:

[{"id":130},{"id":131},{"id":132},{"id":133},{"id":134}]

From everything I have read, it would seem that I need a class that defines the content of my json. 从我阅读的所有内容来看,似乎我需要一个定义json内容的类。 For example: 例如:

class accountTypes
{
    public int id { get; set; }
    public string type { get; set; }
}

From there, it would seem that I only need to create a list of type "accountTypes" and then add each "id" to the list. 从那里开始,似乎只需要创建一个类型为“ accountTypes”的列表,然后将每个“ id”添加到列表中即可。

List<accountTypes> jsonList = new List<accountTypes>();
int numOfChildren = AccountTypesList.Children.Count;
for (int i = 0; i < numOfChildren; i++)
{
    if (((CheckBox)AccountTypesList.Children[i]).IsChecked == true)
    {
        jsonList.Add(new accountTypes() { id = (int)(double)((CheckBox)AccountTypesList.Children[i]).Tag });
    }
}

While I am 99% sure that the above code is very flawed, it does not crash on me, so that is a start at least. 尽管我99%确信上述代码有缺陷,但不会对我造成崩溃,因此至少是一个开始。 What I am struggling with now though is how I would serialize the list "jsonList". 我现在正在努力的是如何序列化列表“ jsonList”。 Everything I have read thus far either points to JSON.net or the JavaScriptSerializer Class, and not Windows.Data.Json. 到目前为止,我阅读的所有内容都指向JSON.net或JavaScriptSerializer类,而不是Windows.Data.Json。 If I could see a simple example on how to serialize json using Windows.Data.Json, then I could at least visualize what is going on with my list and could correct it accordingly. 如果我能看到一个有关如何使用Windows.Data.Json序列化json的简单示例,那么我至少可以直观地看到列表中的内容,并可以进行相应的更正。 That being said, how do I serialize an array or a list using Windows.Data.Json? 话虽这么说,如何使用Windows.Data.Json序列化数组或列表?

First of all, there's no built-in JSON-serializer that handles all the mapping for you. 首先,没有内置的JSON序列化器可以为您处理所有映射。 This is exactly what JSON.NET is doing for you. 这正是JSON.NET为您所做的。 Therefore, you have to take the manual and long way. 因此,您必须走手动和漫长的道路。

To create exactly this result: 要完全创建此结果:

[{"id":130},{"id":131},{"id":132},{"id":133},{"id":134}]

You have to use the JsonArray class. 您必须使用JsonArray类。 For example, pass your jsonList object to a method like this: 例如,将您的jsonList对象传递给这样的方法:

public string ToJson(List<accountTypes> objectList)
{
    var jArray = new JsonArray();
    foreach (var at in objectList)
    {
        jArray.Add(ToJson(at));
    }
    return jArray.ToString();
}

Whereas you use this method to create a JsonObject for your class object itself (as manual step as well): 而您使用此方法为您的类对象本身创建一个JsonObject (以及手动步骤):

public JsonObject ToJson(accountTypes at)
{
    var jObj = new JsonObject();
    jObj.SetNamedValue("id", JsonValue.CreateNumberValue(at.id));
    return jObj;
}

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

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