【问题标题】:Invalid object passed in, member name expected传入的对象无效,需要成员名称
【发布时间】:2014-02-21 06:22:00
【问题描述】:

我正在尝试使用 JavaScriptSerializer.DeserializeObject(line) 对象将 json 字符串转换为 Dictionary。

现在这段代码在我的 Windows 8 机器上完美运行,在我的测试服务器 Windows 2008 Enterprise R2 Service Pack 2 上,但它在 Windows 2008 Standard R2 Service Pack 1 上不工作。

这是我的代码快照:

JavaScriptSerializer objScriptSerializer = new JavaScriptSerializer();
Dictionary<string, object> dictJson = null; 
dictJson = (Dictionary<string, object>)objScriptSerializer.DeserializeObject(line);

传入的对象无效,应为成员名称。 (729): 下面是我验证 json 字符串时有效的 json 字符串。

{ "Action" : "ValidateDistributionGrid",
  "Data" : { "CompanyId" : "iqi",
      "CurrencySignatureOfInvoice" : "",
      "CurrentInvoiceDocType" : "Journal Entry",
      "GenericGridCollection" : [ { "ControlName" : "Distribution",
            "ValueCollection" : [ { "" : "",
                  "acccode" : "199871",
                  "acccodedescr" : "Account Not in Archimedes",
                  "amount" : "123.00",
                  "assetno" : "Affinity Express Philippines Inc.",
                  "branchno" : "Yes",
                  "category" : "Cost Center not in  Archimedes.",
                  "comments" : "ffd",
                  "companyid" : "",
                  "costcenter" : "1090",
                  "incident" : "",
                  "itemdesc" : "ZTest",
                  "lno" : ""
                },
                { "" : "",
                  "acccode" : "",
                  "acccodedescr" : "",
                  "amount" : "123.00",
                  "assetno" : "",
                  "branchno" : "",
                  "category" : "",
                  "comments" : "",
                  "companyid" : "",
                  "costcenter" : "",
                  "incident" : "",
                  "itemdesc" : "Distribution Total",
                  "lno" : ""
                },
                { "" : "",
                  "acccode" : "",
                  "acccodedescr" : "",
                  "amount" : "",
                  "assetno" : "Acclaim (Legacy) Energy SolutionsAffinity Express Philippines Inc.AG Counselors CorporationAlveo Land CorpAmicassa Process Solutions, Inc.ASG Group LimitedAvida Land, CorporationAyala Automotive Holdings CorpAyala CorporationAyala Land, Inc.Ayala Multi-Purpose CooperativeAyala Property Management CorpAyala System Technology, Inc.Bank of Philippine IslandsBillabongBloom EnergyCalgon CarbonCalifornia Pizza KitchenCalifornia United BankCambridge Major Laboratories, Inc.CapitaMallsCebu Holdings, Inc.ConergyCorel CorporationCrohn's & Colitis Foundation of AmericaDream CatchDVS Intele StreamEvolution Hospitality, LLCExtreme NetworksF. MclintocksGlobe Telecom, Inc.GoodwineGrill ConceptsHaas TCM, Inc.Haven Realty CapitalHealth Trust of AmericaInnovative Dining GroupIntegra Business Processing SolutionsIntegrated Microelectronics, Inc.Integrated Microelectronics, Inc.InterDent Service CorporationJamba Juice (Whirl Colorado)JM Smucker Co.Khronos, LLCLiveit Investment Ltd.Makati Development CorporationManila Water CorporationName Not Found1New Horizon Learning CenterOracle (Philippines) CorporationPSi Technologies, Inc.QuiznosRaben MediaRopes & GrayRosa MexicanoRustan Coffee, Inc.Sage Parts Plus, Inc.SBESILKROADSizzler USA Restaurant, IncSpectrum Pharmaceuticals, Inc.Stone & Pewter AccentsSunburst FarmsSupply Chain Consulting Inc.Sweetener ProductsSymetraTelus International Philippines, Inc.The Bank of Tokyo-Mitsubishi UFJ, Ltd.The Four Seasons Hotel ChicagoThe Four Seasons Hotel HoustonThe OrchardThe Philippine American Life and GeneralThe Ritz Carlton Hotel ChicagoTime OutTWB Company, LLCUnited Capital Financials PartnersVeecoVeggie Grill",
                  "branchno" : "YesNo",
                  "category" : "",
                  "comments" : "",
                  "companyid" : "",
                  "costcenter" : "",
                  "incident" : "",
                  "itemdesc" : "",
                  "lno" : ""
                }
              ]
          },
          { "ControlName" : "SpecialInstructions",
            "ValueCollection" : [ { "comment" : "Please scan invoice to client Archimedes instance then return item to IQB Finance.",
                  "companyid" : "iqi",
                  "resolutiondate" : "2/10/2014 4:37:35 PM",
                  "rno" : "1",
                  "si_code" : "10",
                  "userid" : "ajay vishwakarma",
                  "username" : "ajay"
                },
                { "comment" : "",
                  "companyid" : "",
                  "resolutiondate" : "",
                  "rno" : "",
                  "si_code" : "",
                  "userid" : "",
                  "username" : ""
                }
              ]
          }
        ],
      "ImageId" : "11240088",
      "IncidentNumber" : "10357",
      "InvoiceActualAmount" : "343.0000",
      "NextApproverID" : "cden.iqi",
      "OriginalApprovalEmail" : "",
      "PONumber" : "344334",
      "PathName" : "",
      "RowNo" : "2",
      "SortString" : "",
      "Status" : "E",
      "UserApprovalLimit" : "1000"
    }
}

谁能告诉我哪里出错了。

【问题讨论】:

  • 我无法重现您的问题 - 非常适合我。 Windows 7 VS2013。
  • 我现在该怎么办?它与任何更新有关吗?我需要安装在我的另一台机器上。

标签: c# json


【解决方案1】:

好的,我已经通过 3 个不同的在线验证器运行您的 JSON

  1. http://jsonformatter.curiousconcept.com/
  2. http://jsonlint.com/
  3. http://www.freeformatter.com/json-validator.html

我这样做是因为与此错误消息相关的许多其他 Stack Overflow 问题在解析器格式中发现了轻微错误,这导致了错误的发生。如:

  1. Invalid object error when sending json values with apostrophe to webservice
  2. JSON "Invalid object passed in, member name expected"
  3. Parse Google JSON response in VB.NET

我还处理了您的输入:

dictJson = objScriptSerializer.Deserialize<Dictionary<string, object>>(line);

但没有发现任何变化,正如我在评论中所说,我无法重现该问题。

如果它只发生在一台机器上,它一定是环境问题。也许有一个可以应用的 .NET 补丁。例如,这个补丁修复了 JavaScriptSerializer

中看起来非常相似的错误

http://connect.microsoft.com/VisualStudio/feedback/details/775981/kb2656351-javascriptserializer-deserialize-error-operation-is-not-valid-due-to-the-current-state-of-the-object

我认为最好的办法是确保发生故障的机器是最新的所有 .NET 补丁和服务包。

更新

以下是有关如何访问上述补丁的一些详细信息。上面的链接引用了以下 KB - http://support.microsoft.com/kb/2828843。因为它声明它只有在向 MS 提出请求时才可用。但是,该 KB 的后续更新也引用了以下链接:http://support.microsoft.com/kb/2889629/en-us

在该链接上,它指出此补丁可用于以下操作系统服务包:

修补程序汇总 2889629 可用于 Windows 上的 .NET Framework 4 服务器 2003 SP2、Windows XP SP3、Windows Vista SP2、Windows 7 SP1、 Windows Server 2008 SP2 和 Windows Server 2008 R2 SP2

【讨论】:

  • 感谢 Peter R 的回复。即使我认为它与Windows更新有关。再次感谢您抽出时间。我去那个页面下载了那个软件,但是链接不可用,我不知道在哪里可以下载那个软件
  • 我仍然无法解决这个错误?我应该怎么做才能手动获取此更新?我无法自动更新生产服务器 ryt ?
  • 好吧,我安装了 .Net Framework 4.5,它解决了我的问题。因此问题的当前状态已解决。
  • 直到刚才我才注意到你的消息。我很高兴它现在已经解决了。我将更新文章,详细介绍如何下载此补丁。
  • 我的问题也通过安装 .Net 4.5 得到解决。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
相关资源
最近更新 更多