环境:Windows 10 Fall Creators Update上的Visual Studio 2017(1709)
目标:使用DataContractJsonSerializer序列化JSON对象
问题:我无法解决DataContractJsonSerializer。在查看项目引用时,我尝试将System.Runtime.Serializtion(v4.0.30319)添加到我的引用中,但是没有运气。是否需要访问DataContractJsonSerializer的其他参考/程序包。
替代方案:是的,我知道我可以使用NewtonSoft.Json,并且可能会但想要一个结论性的答案,然后再向项目中添加其他依赖项。
研究:我曾尝试过在Google以及SO上寻找SO,但DataContractJsonSerializer仍然受到很高的引用,并且在发生问题的原因上我找不到最不明显的东西。
工程档案
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{53E26A85-0C38-4B64-9C45-7FF41EBF22F5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
输出:
1>------ Build started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
1>C:\Users\amarne\source\repos\ConsoleApp1\ConsoleApp1\Program.cs(24,13,24,39): error CS0246: The type or namespace name 'DataContractJsonSerializer' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\amarne\source\repos\ConsoleApp1\ConsoleApp1\Program.cs(24,50,24,76): error CS0246: The type or namespace name 'DataContractJsonSerializer' could not be found (are you missing a using directive or an assembly reference?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
对Yaakov的感谢,我知道发生了什么。
对于第一次使用DataContractJsonSerializer的用户,请记住在执行该操作后手动将引用添加到System.Runtime.Serialization。
您应该能够解析“使用System.Runtime.Serialization.Json;”。
就我而言,我猜想在添加参考之后,我只是看到VS不满意,因此没有手动构建它。所以我在正确的路上只是没有给它足够的时间:)