字符串转换列表为JSON格式 [英] Convert a list of string into json format

查看:112
本文介绍了字符串转换列表为JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换字符串列表

 List<string> keys = new List<string>() { "1-12VEXP", "1-124DH9"};

要JSON格式一样的:

To json format same as :

[["1-12VEXP"],["1-124DH9"]] 

在.NET。

我使用Newtonsoft.Json。

I'm using Newtonsoft.Json .

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

直线上升序列化是行不通的,因为项目是不等价的。如果你真的想你问的,那么你就需要包含数组的数组,然后序列化数组:

Straight-up serialization won't work, since the items are not equivalent. If you truly want what you're asking for, then you need an array which contains arrays, then serialize that array:

您可以做到这一点,首先转换您的集合,然后简单的JSON序列化:

You can do that by first converting your collection, then simple JSON serialization:

string[][] newKeys = keys.Select(x => new string[]{x}).ToArray();

string json = JsonConvert.SerializeObject(newKeys);

这篇关于字符串转换列表为JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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