c# - 该值不断被循环替换
问题描述
我面临的问题是,json[]
将不断被新价值取代。
我想要实现的是存储所有值。
我已经尝试过json[index][xxx]
,但它仍然无法正常工作。
var json = new JObject();
for(int index = 0; index < employeeCareerProgression.Count; index++)
{
json["Desc"] = employeeCareerProgression[index].Description;
json["Date"] = employeeCareerProgression[index].ProgressionStartDate;
}
解决方案
您必须向您的 JObject 添加一个新的 JProperty - 您正在为每个循环的相同属性设置一个新值。
试试这个,它可能对你有用。不过我还没有测试过。
var json = new JObject();
for(var index = 0; index < employeeCareerProgression.Count; index++)
{
json.Add(new JProperty(index, // You could change this index to a more descriptive field on your employeeCareerProgression object (maybe the primary key, or employee name?)
new JObject
{
new JProperty("Desc", employeeCareerProgression[index].Description),
new JProperty("Date", employeeCareerProgression[index].ProgressionStartDate),
}
);
}
您最终应该得到这样的 JSON:
{[
'0': {
'Desc': 'DESCRIPTION',
'Date': '2020-12-14T00:00:00Z'
},
'1': {
...
}
]}
推荐阅读
- swift - “扩大图书馆的表面积”是什么意思?
- excel - 函数未返回所需值
- python - Python Sql 导入数据字符串
- react-native - React Native camera Tap to foucs not working in android
- xamarin.android - mvvmcross Android. Cannot access a disposed object on Enabled Binding
- jquery - 当我使用引导 css 时,工具提示在 fullcalendar 中不起作用
- wordpress - I try to make a function that hides the payment method title and subtitle when the cart is 0,- while using a coupon by ID
- node.js - Implementing Poloniex crypto using Node.js
- python - 安装python包的一个问题
- ios - 如果一个变异函数设置它自己等于另一个函数,这意味着什么