传入的对象无效,期望成员名称

时间:2014-02-21 06:22:00

标签: c# json

我正在尝试使用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"
    }
}

有人请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:2)

好的,我通过3种不同的在线验证器运行你的JSON

  1. http://jsonformatter.curiousconcept.com/
  2. http://jsonlint.com/
  3. http://www.freeformatter.com/json-validator.html
  4. 我这样做是因为与此错误消息相关的许多其他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
    4. 我还用以下方式处理了您的输入:

      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

      在该链接上,它指出此修补程序在以下OS服务包中可用:

        

      修补程序汇总2889629适用于Windows上的.NET Framework 4   Server 2003 SP2,Windows XP SP3,Windows Vista SP2,Windows 7 SP1,   Windows Server 2008 SP2和Windows Server 2008 R2 SP2