环境:Windows 10 Fall Creators更新中的Visual Studio 2017(1709)
目标:使用DataContractJsonSerializer序列化JSON对象
问题:我无法解析DataContractJsonSerializer。看看y项目参考我尝试将System.Runtime.Serializtion(v4.0.30319)添加到我的参考文献但没有运气。是否需要额外的参考/包来访问DataContractJsonSerializer。
替代:是的,我知道我可以使用NewtonSoft.Json,但在向项目添加额外的依赖项之前可能会想要一个确定的答案。
研究:我试图在Google和Google上使用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 ==========
答案 0 :(得分:1)
对Yaakov来说,我知道发生了什么事。 对于那些第一次使用DataContractJsonSerializer的人,请记住在执行此操作后手动添加对 System.Runtime.Serialization 的引用 你应该能够解决&#34;使用System.Runtime.Serialization.Json;&#34;
在我的情况下,我想在添加引用后,我刚看到VS并不高兴,然后没有手动构建它。所以我在正确的轨道上没有给它足够的时间:))