我正在尝试反序列化JSON字符串并获取空值。我试图反序列化:DataContractJsonSerializer,JavaScriptSerializer,JsonConvert。
此Object返回null:RootObject gr = JsonConvert.DeserializeObject(responseJson);
string apiUrl = "http://data.posccaesar.org/rdl/?output=json&query=%23queryinterface%23%0APREFIX+owl:+%3Chttp:%2F%2Fwww.w3.org%2F2002%2F07%2Fowl%23%3E%0APREFIX+rdf:+%3Chttp:%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX+rdfs:++%3Chttp:%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX+xsd:+%3Chttp:%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0APREFIX+fn:+%3Chttp:%2F%2Fwww.w3.org%2F2005%2Fxpath-functions%23%3E%0APREFIX+p2:+%3Chttp:%2F%2Frds.posccaesar.org%2F2008%2F02%2FOWL%2FISO-15926-2_2003%23%3E%0APREFIX+rds:+%3Chttp:%2F%2Frdl.rdlfacade.org%2Fdata%23%3E%0APREFIX+afn:+%3Chttp:%2F%2Fjena.hpl.hp.com%2FARQ%2Ffunction%23%3E%0APREFIX+dc:+%3Chttp:%2F%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%0A%0APREFIX+rdl:+%3Chttp:%2F%2Fdata.posccaesar.org%2Frdl%2F%3E%0APREFIX+ptrn:+%3Chttp:%2F%2Fdata.posccaesar.org%2Fptrn%2F%3E%0A%0Aselect+%3Fsub+%3Flabel+where+%7B%0A%09%3Fa+p2:hasSuperclass+rdl:RDS608876181+;%0A%09p2:hasSubclass+%3Fsub.%0A%09%3Fsub+rdfs:label+%3Flabel.%0A%7D%0Alimit+1000%0A";
var jsonPayload = new { seriesid = new[] { "LAUCN040010000000005", "LAUCN040010000000006" } };
string requestJson = JsonConvert.SerializeObject(jsonPayload);
string responseJson = string.Empty;
using (WebClient client = new WebClient())
{
byte[] response = client.UploadData(apiUrl, Encoding.UTF8.GetBytes(requestJson));
responseJson = Encoding.UTF8.GetString(response);
}
RootObject gr = JsonConvert.DeserializeObject<RootObject>(responseJson);
我从http://json2csharp.com/生成课程,我得到如下。
using System.Collections.Generic;
public class Graph
{
public string @id { get; set; }
public string @type { get; set; }
public string defaultRdsId { get; set; }
public string hasCreationDate { get; set; }
public string hasCreator { get; set; }
public string hasDefinition { get; set; }
public string hasDesignation { get; set; }
public string hasIdPCA { get; set; }
public string hasStatus { get; set; }
public string label { get; set; }
public string sameAs { get; set; }
public string hasDesignationAbbrev { get; set; }
public string hasNote { get; set; }
public string hasRegistrar { get; set; }
public string hasRegistrarAuth { get; set; }
public string hasSubclass { get; set; }
public string hasSuperclass { get; set; }
public string rdsWipEquivalent { get; set; }
}
public class SameAs
{
public string @id { get; set; }
public string @type { get; set; }
}
public class HasSuperclass
{
public string @id { get; set; }
public string @type { get; set; }
}
public class HasSubclass
{
public string @id { get; set; }
public string @type { get; set; }
}
public class RdsWipEquivalent
{
public string @id { get; set; }
public string @type { get; set; }
}
public class Context
{
public string hasDesignation { get; set; }
public string hasNote { get; set; }
public string hasCreator { get; set; }
public string hasDefinition { get; set; }
public string label { get; set; }
public string hasIdPCA { get; set; }
public string hasCreationDate { get; set; }
public string hasStatus { get; set; }
public string defaultRdsId { get; set; }
public string hasDesignationAbbrev { get; set; }
public SameAs sameAs { get; set; }
public HasSuperclass hasSuperclass { get; set; }
public HasSubclass hasSubclass { get; set; }
public string hasRegistrarAuth { get; set; }
public string hasRegistrar { get; set; }
public RdsWipEquivalent rdsWipEquivalent { get; set; }
public string rds { get; set; }
public string p2 { get; set; }
public string owl { get; set; }
public string rdf { get; set; }
public string afn { get; set; }
public string xsd { get; set; }
public string fn { get; set; }
public string oldrdl { get; set; }
public string rdfs { get; set; }
public string rdl { get; set; }
public string list { get; set; }
public string dc { get; set; }
}
public class RootObject
{
public List<Graph> @graph { get; set; }
public Context @context { get; set; }
}
我的JSON字符串:
{
"@graph" : [ {
"@id" : "rdl:RDS14186420",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R59065461445",
"hasCreationDate" : "2003.10.30",
"hasCreator" : "u83130",
"hasDefinition" : "A component class whose members are classes of electrical heater components.",
"hasDesignation" : "ELECTRICAL HEATER COMPONENT CLASS",
"hasIdPCA" : "RDS14186420",
"hasStatus" : "Recorded",
"label" : "ELECTRICAL HEATER COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS14186420"
}, {
"@id" : "rdl:RDS15378839",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R36079680064",
"hasCreationDate" : "2004.05.18",
"hasCreator" : "u82237",
"hasDefinition" : "A class whose members are intended to form part of a ship hull or other structural parts.",
"hasDesignation" : "SHIP STRUCTURAL COMPONENT CLASS",
"hasIdPCA" : "RDS15378839",
"hasStatus" : "Recorded",
"label" : "SHIP STRUCTURAL COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS15378839"
}, {
"@id" : "rdl:RDS17055682",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R90167534107",
"hasCreationDate" : "2005.09.30",
"hasCreator" : "u20683",
"hasDefinition" : "Classes of machine components that forms part of of a drilling fluid powered downhole drilling motor.",
"hasDesignation" : "DOWNHOLE DRILLING MOTOR COMPONENT CLASS",
"hasIdPCA" : "RDS17055682",
"hasStatus" : "Recorded",
"label" : "DOWNHOLE DRILLING MOTOR COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS17055682"
}, {
"@id" : "rdl:RDS17056232",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R87192971881",
"hasCreationDate" : "2005.11.08",
"hasCreator" : "u20683",
"hasDefinition" : "The class \"heat exchanger component class\" is the subclass of the class \"mechanical equipment class\" that contains each mechanical equipment component class for which each member can be a component of a heat exchanger.",
"hasDesignation" : "HEAT EXCHANGER COMPONENT CLASS",
"hasDesignationAbbrev" : "1705623200",
"hasIdPCA" : "RDS17056232",
"hasNote" : "A more informal definition is \"A heat exchanger component class is a heat transfer equipment component class that has members which can be components of a heat exchanger\".",
"hasStatus" : "Incomplete",
"label" : "HEAT EXCHANGER COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS17056232"
}, {
"@id" : "rdl:RDS17056700",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R36377127353",
"hasCreationDate" : "2005.10.05",
"hasCreator" : "u82237",
"hasDefinition" : "A component class whose members are classes of parts of classes of process plant equipment.",
"hasDesignation" : "EQUIPMENT COMPONENT CLASS",
"hasIdPCA" : "RDS17056700",
"hasStatus" : "Incomplete",
"label" : "EQUIPMENT COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS17056700"
}, {
"@id" : "rdl:RDS201644",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R30951219061",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "A class whose members are classes of artefacts.",
"hasDesignation" : "ARTEFACT CLASS",
"hasIdPCA" : "RDS201644",
"hasRegistrar" : "mvsen",
"hasRegistrarAuth" : "RDS",
"hasStatus" : "Qualified",
"label" : "ARTEFACT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS201644"
}, {
"@id" : "rdl:RDS201914",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R58281768156",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "Classes of machine components that forms parts of a coupling.",
"hasDesignation" : "POWER TRANSMISSION COUPLING COMPONENT CLASS",
"hasIdPCA" : "RDS201914",
"hasStatus" : "Recorded",
"label" : "POWER TRANSMISSION COUPLING COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS201914"
}, {
"@id" : "rdl:RDS203399",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R51597621609",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "Classes of instrument components that forms parts of an instrument",
"hasDesignation" : "INSTRUMENT COMPONENT CLASS",
"hasIdPCA" : "RDS203399",
"hasStatus" : "Recorded",
"label" : "INSTRUMENT COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS203399"
}, {
"@id" : "rdl:RDS203489",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R15440214689",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "A heating, ventilation and air conditioning (HVAC) equipment class that contains classes of equipment used as parts of HVAC equipment",
"hasDesignation" : "HVAC COMPONENT CLASS",
"hasIdPCA" : "RDS203489",
"hasStatus" : "Recorded",
"label" : "HVAC COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS203489"
}, {
"@id" : "rdl:RDS203534",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R29782655361",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "A rotating mechanical equipment class that contains classes of machine components that forms parts of a pump.",
"hasDesignation" : "PUMP COMPONENT CLASS",
"hasIdPCA" : "RDS203534",
"hasStatus" : "Recorded",
"label" : "PUMP COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS203534"
}, {
"@id" : "rdl:RDS220589",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R39099110350",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "Classes of components that forms parts of a gearbox.",
"hasDesignation" : "GEARBOX COMPONENT CLASS",
"hasIdPCA" : "RDS220589",
"hasStatus" : "Recorded",
"label" : "GEARBOX COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS220589"
}, {
"@id" : "rdl:RDS224684",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R90018669619",
"hasCreationDate" : "2005.10.25",
"hasCreator" : "u83130",
"hasDefinition" : "A valve equipment class that contains classes of valve components or parts used in pumps and pump assemblies. Only to be used during the classification of valves.",
"hasDesignation" : "VALVE COMPONENT CLASS",
"hasIdPCA" : "RDS224684",
"hasStatus" : "Incomplete",
"label" : "VALVE COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS224684"
}, {
"@id" : "rdl:RDS225314",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R60279376190",
"hasCreationDate" : "2006.06.15",
"hasCreator" : "u20683",
"hasDefinition" : "A process piping equipment class that contains classes of equipment used in or in connection with a piping system.",
"hasDesignation" : "PIPING COMPONENT CLASS",
"hasIdPCA" : "RDS225314",
"hasStatus" : "Recorded",
"label" : "PIPING COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS225314"
}, {
"@id" : "rdl:RDS226124",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R20688513774",
"hasCreationDate" : "2001.10.16",
"hasCreator" : "u82237",
"hasDefinition" : "Classes of components that forms parts of a mechanical seal.",
"hasDesignation" : "MECHANICAL SEAL COMPONENT CLASS",
"hasIdPCA" : "RDS226124",
"hasStatus" : "Recorded",
"label" : "MECHANICAL SEAL COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS226124"
}, {
"@id" : "rdl:RDS608876181",
"@type" : "p2:ClassOfClassOfIndividual",
"defaultRdsId" : "R63534165926",
"hasCreationDate" : "2007.10.22",
"hasCreator" : "mvsen",
"hasDefinition" : "An artefact class whose members are suitable as a part of a larger artefact.",
"hasDesignation" : "COMPONENT CLASS",
"hasIdPCA" : "RDS608876181",
"hasStatus" : "Incomplete",
"label" : "COMPONENT CLASS",
"sameAs" : "http://posccaesar.org/rdl/RDS608876181"
}, {
"@id" : "rdl:RDS608877101",
"@type" : "p2:Specialization",
"defaultRdsId" : "R29442817194",
"hasIdPCA" : "RDS608877101",
"hasSubclass" : "rdl:RDS608876181",
"hasSuperclass" : "rdl:RDS201644",
"sameAs" : "http://posccaesar.org/rdl/RDS608877101"
}, {
"@id" : "rdl:RDS608880141",
"@type" : "p2:Specialization",
"defaultRdsId" : "R72593201380",
"hasIdPCA" : "RDS608880141",
"hasSubclass" : "rdl:RDS14186420",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS608880141"
}, {
"@id" : "rdl:RDS609008651",
"@type" : "p2:Specialization",
"defaultRdsId" : "R61353051370",
"hasIdPCA" : "RDS609008651",
"hasSubclass" : "rdl:RDS17055682",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609008651"
}, {
"@id" : "rdl:RDS609008911",
"@type" : "p2:Specialization",
"defaultRdsId" : "R72794538375",
"hasIdPCA" : "RDS609008911",
"hasSubclass" : "rdl:RDS17056700",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609008911"
}, {
"@id" : "rdl:RDS609009131",
"@type" : "p2:Specialization",
"defaultRdsId" : "R91377360705",
"hasIdPCA" : "RDS609009131",
"hasSubclass" : "rdl:RDS220589",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609009131"
}, {
"@id" : "rdl:RDS609009331",
"@type" : "p2:Specialization",
"defaultRdsId" : "R68426307879",
"hasIdPCA" : "RDS609009331",
"hasSubclass" : "rdl:RDS17056232",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609009331"
}, {
"@id" : "rdl:RDS609009541",
"@type" : "p2:Specialization",
"defaultRdsId" : "R55261332198",
"hasIdPCA" : "RDS609009541",
"hasSubclass" : "rdl:RDS203489",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609009541"
}, {
"@id" : "rdl:RDS609009681",
"@type" : "p2:Specialization",
"defaultRdsId" : "R10066878943",
"hasIdPCA" : "RDS609009681",
"hasSubclass" : "rdl:RDS203399",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609009681"
}, {
"@id" : "rdl:RDS609009931",
"@type" : "p2:Specialization",
"defaultRdsId" : "R91203774026",
"hasIdPCA" : "RDS609009931",
"hasSubclass" : "rdl:RDS226124",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609009931"
}, {
"@id" : "rdl:RDS609011171",
"@type" : "p2:Specialization",
"defaultRdsId" : "R58289922054",
"hasIdPCA" : "RDS609011171",
"hasSubclass" : "rdl:RDS225314",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609011171"
}, {
"@id" : "rdl:RDS609011401",
"@type" : "p2:Specialization",
"defaultRdsId" : "R40124754568",
"hasIdPCA" : "RDS609011401",
"hasSubclass" : "rdl:RDS201914",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609011401"
}, {
"@id" : "rdl:RDS609011651",
"@type" : "p2:Specialization",
"defaultRdsId" : "R65525751798",
"hasIdPCA" : "RDS609011651",
"hasSubclass" : "rdl:RDS203534",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609011651"
}, {
"@id" : "rdl:RDS609011921",
"@type" : "p2:Specialization",
"defaultRdsId" : "R35776609616",
"hasIdPCA" : "RDS609011921",
"hasSubclass" : "rdl:RDS15378839",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609011921"
}, {
"@id" : "rdl:RDS609012311",
"@type" : "p2:Specialization",
"defaultRdsId" : "R91523122738",
"hasIdPCA" : "RDS609012311",
"hasSubclass" : "rdl:RDS224684",
"hasSuperclass" : "rdl:RDS608876181",
"sameAs" : "http://posccaesar.org/rdl/RDS609012311"
}, {
"@id" : "rds:R63534165926",
"rdsWipEquivalent" : "rdl:RDS608876181"
} ],
"@context" : {
"hasDesignation" : "http://data.posccaesar.org/rdl/hasDesignation",
"hasNote" : "http://data.posccaesar.org/rdl/hasNote",
"hasCreator" : "http://data.posccaesar.org/rdl/hasCreator",
"hasDefinition" : "http://data.posccaesar.org/rdl/hasDefinition",
"label" : "http://www.w3.org/2000/01/rdf-schema#label",
"hasIdPCA" : "http://data.posccaesar.org/rdl/hasIdPCA",
"hasCreationDate" : "http://data.posccaesar.org/rdl/hasCreationDate",
"hasStatus" : "http://data.posccaesar.org/rdl/hasStatus",
"defaultRdsId" : "http://data.posccaesar.org/rdl/defaultRdsId",
"hasDesignationAbbrev" : "http://data.posccaesar.org/rdl/hasDesignationAbbrev",
"sameAs" : {
"@id" : "http://www.w3.org/2002/07/owl#sameAs",
"@type" : "@id"
},
"hasSuperclass" : {
"@id" : "http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#hasSuperclass",
"@type" : "@id"
},
"hasSubclass" : {
"@id" : "http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#hasSubclass",
"@type" : "@id"
},
"hasRegistrarAuth" : "http://data.posccaesar.org/rdl/hasRegistrarAuth",
"hasRegistrar" : "http://data.posccaesar.org/rdl/hasRegistrar",
"rdsWipEquivalent" : {
"@id" : "http://data.posccaesar.org/rdl/rdsWipEquivalent",
"@type" : "@id"
},
"rds" : "http://rdl.rdlfacade.org/data#",
"p2" : "http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#",
"owl" : "http://www.w3.org/2002/07/owl#",
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"afn" : "http://jena.hpl.hp.com/ARQ/function#",
"xsd" : "http://www.w3.org/2001/XMLSchema#",
"fn" : "http://www.w3.org/2005/xpath-functions#",
"oldrdl" : "http://rds.posccaesar.org/2008/06/OWL/RDL#",
"rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
"rdl" : "http://data.posccaesar.org/rdl/",
"list" : "http://www.co-ode.org/ontologies/list.owl#",
"dc" : "http://purl.org/dc/elements/1.1/"
}
}
为什么它为空?
由于网络浏览器中的Cookie或登录信息?
答案 0 :(得分:0)
当我要求Visual Studio将您的JSON转换为类时,我得到一个不同的输出(见下文)。试一试,看看你是否有更多运气。
如果您想亲自尝试一下,我认为此功能已添加到 Visual Studio 2013 (如果我记得不正确,有人会更正我)并且它在下面编辑&gt;选择性粘贴。要将JSON转换为Class,您需要在面向.NET 3.5+的项目中,对于XML到Class,您需要.NET 4.5 +。
public class Rootobject
{
public Graph[] graph { get; set; }
public Context context { get; set; }
}
public class Context
{
public string hasDesignation { get; set; }
public string hasNote { get; set; }
public string hasCreator { get; set; }
public string hasDefinition { get; set; }
public string label { get; set; }
public string hasIdPCA { get; set; }
public string hasCreationDate { get; set; }
public string hasStatus { get; set; }
public string defaultRdsId { get; set; }
public string hasDesignationAbbrev { get; set; }
public Sameas sameAs { get; set; }
public Hassuperclass hasSuperclass { get; set; }
public Hassubclass hasSubclass { get; set; }
public string hasRegistrarAuth { get; set; }
public string hasRegistrar { get; set; }
public Rdswipequivalent rdsWipEquivalent { get; set; }
public string rds { get; set; }
public string p2 { get; set; }
public string owl { get; set; }
public string rdf { get; set; }
public string afn { get; set; }
public string xsd { get; set; }
public string fn { get; set; }
public string oldrdl { get; set; }
public string rdfs { get; set; }
public string rdl { get; set; }
public string list { get; set; }
public string dc { get; set; }
}
public class Sameas
{
public string id { get; set; }
public string type { get; set; }
}
public class Hassuperclass
{
public string id { get; set; }
public string type { get; set; }
}
public class Hassubclass
{
public string id { get; set; }
public string type { get; set; }
}
public class Rdswipequivalent
{
public string id { get; set; }
public string type { get; set; }
}
public class Graph
{
public string id { get; set; }
public string type { get; set; }
public string defaultRdsId { get; set; }
public string hasCreationDate { get; set; }
public string hasCreator { get; set; }
public string hasDefinition { get; set; }
public string hasDesignation { get; set; }
public string hasIdPCA { get; set; }
public string hasStatus { get; set; }
public string label { get; set; }
public string sameAs { get; set; }
public string hasDesignationAbbrev { get; set; }
public string hasNote { get; set; }
public string hasRegistrar { get; set; }
public string hasRegistrarAuth { get; set; }
public string hasSubclass { get; set; }
public string hasSuperclass { get; set; }
public string rdsWipEquivalent { get; set; }
}