JSON deserialize with array and non-Array -
i have json string. has part this
"result": { "additionalinfo": { "subcategoryid": "978", "sellerpartnumber": "04-vo7t-14pp", "manufacturerpartnumberorisbn": "04-vo7t-14pp", "upc": null }, "errorlist": { "errordescription": [ { "#cdata-section": "error(s). item not created." }, { "#cdata-section": "error:[item not exist. please create new list.]" } ] } }
however, part array well. like
"result": [ { "additionalinfo": { "subcategoryid": "1512", "sellerpartnumber": "ty-szt1-358v", "manufacturerpartnumberorisbn": "ty-szt1-358v", "upc": null }, "errorlist": { "errordescription": [ { "cdata-section": "error(s). item not created." }, { "cdata-section": "cufflinkmaterial - property: [cufflinkmaterial] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." }, { "cdata-section": "cufflinktype - property: [cufflinktype] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." } ] } }, { "additionalinfo": { "subcategoryid": "1512", "sellerpartnumber": "uf-t05c-t6xg", "manufacturerpartnumberorisbn": "uf-t05c-t6xg", "upc": null }, "errorlist": { "errordescription": [ { "cdata-section": "error(s). item not created." }, { "cdata-section": "cufflinkmaterial - property: [cufflinkmaterial] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." }, { "cdata-section": "cufflinktype - property: [cufflinktype] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." } ] } }, { "additionalinfo": { "subcategoryid": "1512", "sellerpartnumber": "5b-1137-wt3o", "manufacturerpartnumberorisbn": "5b-1137-wt3o", "upc": null }, "errorlist": { "errordescription": [ { "cdata-section": "error(s). item not created." }, { "cdata-section": "cufflinkmaterial - property: [cufflinkmaterial] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." }, { "cdata-section": "cufflinktype - property: [cufflinktype] value error. submitted value: [mps.domain.subcategoryproperty] not recognized property." } ] } } ]
is there generic way can deserlize both json without defining different objects? like, define array of result object, , when there single entity, create 1 index in object array, , when there multiple, may create multiple indexes in array.
ultimate goal utilize object object , utilize parse both single result object or multiple.
is possible? sameers
thanks brian rogers
the reply in link.
how handle both single item , array same property using json.net
i converted vb.net using code.
class singleorarrayconverter(of t) inherits newtonsoft.json.jsonconverter public overrides function canconvert(byval objecttype type) boolean 'return (objecttype = gettype(list(of t))) homecoming objecttype.equals(gettype(generic.list(of t))) end function public overrides function readjson(byval reader newtonsoft.json.jsonreader, byval objecttype type, byval existingvalue object, byval serializer newtonsoft.json.jsonserializer) object dim token newtonsoft.json.linq.jtoken = newtonsoft.json.linq.jtoken.load(reader) if token.type = newtonsoft.json.linq.jtokentype.array homecoming token.toobject(of generic.list(of t))() end if dim list new generic.list(of t) list.add(token.toobject(of t)()) homecoming list end function public overrides readonly property canwrite() boolean homecoming false end end property public overrides sub writejson(byval author newtonsoft.json.jsonwriter, byval value object, byval serializer newtonsoft.json.jsonserializer) throw new notimplementedexception() end sub end class
and applied attribute info definition.
<newtonsoft.json.jsonproperty("result")> _ <newtonsoft.json.jsonconverter(gettype(utilities.jsonutilities.singleorarrayconverter(of feedresulttype)))> _ public property result() generic.list(of feedresulttype) homecoming m_result end set(byval value generic.list(of feedresulttype)) m_result = value end set end property
and now. sameers
yes, using newtonsoft library
arrays json vb.net
No comments:
Post a Comment