【问题标题】:LitJSON error in powershellPowershell中的LitJSON错误
【发布时间】:2013-05-28 19:11:53
【问题描述】:

我正在尝试使用来自 PowerShell 的 Leonardo B 的 litjson 库。我需要使用的库的入口点是静态方法。我可以使用 add-type 加载程序集,没问题。尝试使用静态方法会出现此错误:

PS C:\Users\david>[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")
format-default : The JsonData instance has to be initialized first
    + CategoryInfo          : NotSpecified: (:) [format-default], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.FormatDefaultCommand

我虽然发生的事情是该课程的 static constructor 没有运行,但 Lars Truijens 纠正了这种误解。该错误是 JsonData 类的异常。虽然同一行代码在 VS2012(针对 .net 3.5)中可以正常工作,但它不会在 powershell 中运行。

更新:

此单元测试在 Visual Studio 2012 中通过:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LitJson;

namespace litjsonTest
{
    [TestClass]
    public class TestJsonMapper
    {
        [TestMethod]
        public void TestMethod1()
        {
            var obj = LitJson.JsonMapper.ToObject("{\"foo\":\"bar\"}");
            Assert.IsNotNull(obj);
        }
    }
}

【问题讨论】:

标签: c# powershell static .net-assembly litjson


【解决方案1】:

那将完全破坏 .Net。我不信。甚至更多,因为错误来自 JsonData 类,并且是因为没有从该类的(非静态)构造函数设置类型字段。请参阅http://couchbrowse.googlecode.com/svn/trunk/LitJson/JsonData.cs 您确定您的 LitJson 代码正确吗?这个确切的代码在 C# 程序中有效吗?

更新

你确定你的 Json 是正确的吗?您发布的示例不是。应该有双引号。尝试使用反引号转义或使用here-string

[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")

【讨论】:

  • 感谢您更正 JSON 语法,但我遇到了同样的错误。今天晚些时候我会尝试调试这个库。
  • 将此添加到示例中。今天晚些时候我将尝试将调试器连接到 PS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多