[英]Convert Array into JSON String in ASP.NET C#
I have a simple array that looks like this: 我有一个看起来像这样的简单数组:
{"image_01.jpg","image_02.jpg","image_03.jpg"}
I need to format it into this: 我需要将其格式化为:
[{"small":"image_01.jpg","big":"image_01.jpg"},{"small":"image_02.jpg","big":"image_02.jpg"}, {"small":"image_03.jpg","big":"image_03.jpg"}]
I am very new to ASP.NET C#, I've been trying the examples here: https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx but I am not sure how to implement them into my array. 我是ASP.NET C#的新手,我一直在这里尝试示例: https : //msdn.microsoft.com/zh-cn/library/2yd9wwz4.aspx,但是我不确定如何将它们实现到数组中。
you need to import the System.Linq
and System.Web.Script.Serialization
namespaces and then use the following code: 您需要导入
System.Linq
和System.Web.Script.Serialization
命名空间,然后使用以下代码:
var array = new string[] { "image_01.jpg", "image_02.jpg", "image_03.jpg" };
var newArray = array.Select(x => new { small = x, big = x }).ToArray();
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(newArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.