C#-尝试将Dictionary转换为JSON - c#

这是我的代码:

public class JsonData
{
    public string header;
    public Dictionary<string, string> data = new Dictionary<string, string>();
    public int connectionId;
}

JsonData SendData = new JsonData();
SendData.header = "01";
SendData.data.Add("text", "What is Lorem Ipsum?Lorem ");
SendData.data.Add("accId", "123");
SendData.connectionId = 12;

string json = JsonUtility.ToJson(SendData);

一切都很好,但是以某种方式,转换后的JSON字符串中的键data根本不会出现。

string json看起来像这样:

{"header":"01","connectionId":12}

代替:

{"header":"01","data":{"text":"What is Lorem Ipsum?Lorem ","accId":"123"},"connectionId":12}

我的错误在哪里?
为什么我不能收到这样的结果?

参考方案

使用Newtonsoft.Json获取正确的json。给定您在项目上引用的名称,请使用名称空间:

using Newtonsoft.Json;

并使用JsonConvert.SerializeObject静态方法:

string json = JsonConvert.SerializeObject(SendData);

JSON PATH字段NULL检查表达式 - java

我有一个像bellow的json数组:{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sa…

json_encode的特定方式 - php

所以这是我的PHP<?php include 'connect.php'; $sql = "SELECT name from startup_schema"; $result = mysqli_query($mysqli, $sql) or die("Error in Selecting " …

json_encode网址失败 - php

有人在this bug附近吗?echo json_encode(array('url'=>'/foo/bar')); {"url":"\/foo\/bar"} 我使用Zend_Json and Zend_Json_Expr以便我甚至可以在js对象中获取回调函数-但我无法获得…

json.dumps弄乱顺序 - python

我正在使用json module创建一个包含类似条目的json文件json.dumps({"fields": { "name": "%s", "city": "%s", "status": "%s", "cou…

这个json格式正确吗? - c#

我尝试解析时有json数据,返回错误的语法错误,请帮助我发现语法错误。[{"isData":"Yes","Details":"[{"Id":"70","Name":"Test","FileName&#…