You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将JArray转换为指定格式的字符串?

解决JArray转指定格式字符串的问题

针对你需要将JArray转换为'Apple', 'Pineapple', 'Banana', 'Orange'格式字符串的需求,这里提供两种实用的实现方式(基于C# + Newtonsoft.Json):

方法一:基础实现

直接通过LINQ遍历JArray元素,为每个元素添加单引号后,用string.Join拼接:

using Newtonsoft.Json.Linq;

// 初始化JArray
JArray jaFruits = new JArray("Apple", "Pineapple", "Banana", "Orange");
// 转换为目标格式字符串
string strFruits = string.Join(", ", jaFruits.Select(item => $"'{item.ToString()}'"));

方法二:带单引号转义的安全实现

如果你的JArray元素中可能包含单引号,建议添加转义处理,避免格式错误:

using Newtonsoft.Json.Linq;

JArray jaFruits = new JArray("Apple", "Pineapple", "Banana", "Orange");
string strFruits = string.Join(", ", jaFruits.Select(item => $"'{item.ToString().Replace("'", "\\'")}'"));

内容的提问来源于stack exchange,提问作者Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:18:36