c# - 将两个单独的数据字段存储到一个数组中 c# Array Json from Class
问题描述
嗨,我有以下代码,在我的课堂上,我已经定义了JSON
为最终的 put 请求构建字符串所需的所有类。我正在尝试将我的AttributeColor
和AttributeSize
字段放入该属性string[]
中,以便JSON
返回以下内容:
{"group":null,"productid":"42","sku":"211","money":"20.00","categoryid":"42","attributes":["42","green"]}
myObject.attributes = reader["Sizeattribute"].ToString() + reader["ColorAttribute"].ToString();
我哪里错了?如何将两个字段添加到这个数组以获得完美的 JSON?现在我收到错误无法将类型隐式转换'string'
为'string[]'
代码片段:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace JSON_Example
{
class Program
{
static void Main(string[] args)
{
// Read From SQL
using (SqlConnection connection = new SqlConnection("Server = localhost;Database=xxxx;UID=admin;PASSWORD=xxxx"))
{
String query = "SELECT * FROM [test].[dbo].[DimProductVariantXXX] "; // Please Change the View that you are pointing towards
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
List<Product> myObjectList = new List<Product>();
var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Product myObject = new Product();
myObject.productid = reader["productid"].ToString();
myObject.sku = reader["sku"].ToString();
myObject.money = reader["money"].ToString();
// myObject.categoryid = Convert.ToString(reader["categoryid"]);
myObject.attributes = reader["Sizeattribute"].ToString() + reader["ColorAttribute"].ToString();
// myObject.variantparentid = reader["variantparentid"].ToString();
myObjectList.Add(myObject);
}
}
Console.WriteLine(myObjectList);
Product product = new Product();
String JSONresult = JsonConvert.SerializeObject(product);
string path = @"D:\json\product.json";
if (File.Exists(path))
{
File.Delete(path);
using (var tw = new StreamWriter(path, true))
{
tw.WriteLine(JSONresult.ToString());
tw.Close();
}
}
else if (!File.Exists(path))
{
using (var tw = new StreamWriter(path, true))
{
tw.WriteLine(JSONresult.ToString());
tw.Close();
}
}
}
}
}
}
class Product
{
public Group group;
public string productid { get; set; }
public string sku { get; set; }
public string money { get; set; }
public string categoryid { get; set; }
public string sizeattribute { get; set; }
public string colorattribute { get; set; }
public List<string>[] attributes { get; set; }
}
}
解决方案
在Product
类中,attributes
属性应定义为单个列表或数组。它目前被定义为一个数组List<string>
(不确定这是否是一个错字)。
更新定义:
public List<string> attributes { get; set; }
现在,myObject.attributes
是一个List<string>
,所以您需要初始化列表并将值添加到其中。
您可以使用Collection Initializer执行此操作:
myObject.attributes = new List<string>
{
reader["Sizeattribute"].ToString(),
reader["ColorAttribute"].ToString()
};
这应该会产生预期的 JSON。
推荐阅读
- python - 使用 Windows 调度程序时 Python 脚本找不到文件
- odbc - [Snowflake-ODBC]无法使用 SQLColumns API 成功获取列信息
- java - 如何更改菜单标题颜色android java
- python - 使用循环分支 Apache Beam 管道
- java - 在 Java 中使用 SOAP 工件
- spring-boot - 我可以对两个表使用完全相同的实体 JPA HIBERNATE
- cucumber - 将 cucumber-jvm 从版本 4 升级到版本 6 时是否需要额外的上下文配置?
- asp.net-core - 通过 Blazor WASM 使用 Spotify 身份验证
- python - Youtube 抓取(视频列表)
- php - php 7.3 上 foreach 上的数组到字符串转换错误,但适用于 php 5.6