2014-02-20 77 views
0

我想从这里使用JSON字符串生成C#类http://json2csharp.com/这很好。但我无法解析JSON到网站生成的对象。从Json字符串生成C#对象并将Json字符串解析为生成的对象

这里是JSON字符串

{ 
    "searchParameters":{ 
    "key":"**********", 
    "system":"urn:oid:.8" 
    }, 
    "message":" found one Person matching your search criteria.", 
    "_links":{ 
    "self":{ 
     "href":"https://integration.rest.api.test.com/v1/person?key=123456&system=12.4.34.." 
    } 
    }, 
    "_embedded":{ 
    "person":[ 
     { 
     "details":{ 
      "address":[ 
      { 
       "line":["5554519 testdr"], 
       "city":"testland", 
       "state":"TT", 
       "zip":"12345", 
       "period":{ 
       "start":"2003-10-22T00:00:00Z", 
       "end":"9999-12-31T23:59:59Z" 
       } 
      } 
      ], 
      "name":[ 
      { 
       "use":"usual", 
       "family":["BC"], 
       "given":["TWO"], 
       "period":{ 
       "start":"9999-10-22T00:00:00Z", 
       "end":"9999-12-31T23:59:59Z" 
       } 
      } 
      ], 
      "gender":{ 
      "code":"M", 
      "display":"Male" 
      }, 
      "birthDate":"9999-02-03T00:00:00Z", 
      "identifier":[ 
      { 
       "use":"unspecified", 
       "system":"urn:oid:2.19.8", 
       "key":"", 
       "period":{ 
       "start":"9999-10-22T00:00:00Z", 
       "end":"9999-12-31T23:59:59Z" 
       } 
      } 
      ], 
      "telecom":[ 
      { 
       "system":"email", 
       "value":"[email protected]", 
       "use":"unspecified", 
       "period":{ 
       "start":"9999-10-22T00:00:00Z", 
       "end":"9999-12-31T23:59:59Z" 
       } 
      } 
      ], 
      "photo":[ 
      { 
       "content":{ 
       "contentType":"image/jpeg", 
       "language":"", 
       "data":"", 
       "size":0, 
       "hash":"", 
       "title":"My Picture" 
       } 
      } 
      ] 
     }, 
     "enrolled":true, 
     "enrollmentSummary":{ 
      "dateEnrolled":"9999-02-07T21:39:11.174Z", 
      "enroller":"test Support" 
     }, 
     "_links":{ 
      "self":{ 
      "href":"https://integration.rest.api.test.com/v1/person/-182d-4296-90cc" 
      }, 
      "unenroll":{ 
      "href":"https://integration.rest.api.test.com/v1/person/1b018dc4-182d-4296-90cc-/unenroll" 
      }, 
      "personLink":{ 
      "href":"https://integration.rest.api.test.com/v1/person/-182d-4296-90cc-953c/personLink" 
      }, 
      "personMatch":{ 
      "href":"https://integration.rest.api.commonwellalliance.org/v1/person/-182d-4296-90cc-/personMatch?orgId=" 
      } 
     } 
     } 
    ] 
    } 
} 

下面是我用来转换为对象的代码。

JavaScriptSerializer js = new JavaScriptSerializer(); 
var xx = (PersonsearchVM)js.Deserialize(jsonstr, typeof(PersonsearchVM)); 

是否有任何其他wat来生成对象和解析?

+1

在实际的JSON数据中是换行符还是为了本文的可读性?如果前者,换行符在值之间是正确的,但在字符串值内无效。他们需要在这些情况下逃脱 - '“\ r \ n”'。 –

+0

实际的JSON数据中没有换行符。 – Gokul

回答

0

你是不是铸造合适的对象。将其转换为类型RootObject。

例如

JavaScriptSerializer js = new JavaScriptSerializer(); 
var xx = (RootObject)js.Deserialize(jsonstr, typeof(RootObject)); 

的那JSON 2 CSHARP创建码是这样的:

public class SearchParameters 
{ 
    public string key { get; set; } 
    public string system { get; set; } 
} 

    public class Self 
{ 
    public string href { get; set; } 
} 

public class LinKs 
{ 
    public Self self { get; set; } 
} 

public class Period 
{ 
    public string start { get; set; } 
    public string end { get; set; } 
} 

public class Address 
{ 
    public List<string> line { get; set; } 
    public string city { get; set; } 
    public string __invalid_name__state { get; set; } 
    public string zip { get; set; } 
    public Period period { get; set; } 
} 

public class PerioD2 
{ 
    public string start { get; set; } 
    public string end { get; set; } 
} 

public class Name 
{ 
    public string use { get; set; } 
    public List<string> family { get; set; } 
    public List<string> given { get; set; } 
    public PerioD2 __invalid_name__perio 
d { get; set; } 
} 

public class Gender 
{ 
    public string __invalid_name__co 
de { get; set; } 
    public string display { get; set; } 
} 

public class Period3 
{ 
    public string start { get; set; } 
    public string __invalid_name__end { get; set; } 
} 

public class Identifier 
{ 
    public string use 
{ get; set; } 
    public string system { get; set; } 
    public string key { get; set; } 
    public Period3 period { get; set; } 
} 

public class Period4 
{ 
    public string start { get; set; } 
    public string end { get; set; } 
} 

public class Telecom 
{ 
    public string system { get; set; } 
    public string value { get; set; } 
    public string use { get; set; } 
    public Period4 period { get; set; } 
} 

public class Content 
{ 
    public string contentType { get; set; } 
    public string language { get; set; } 
    public string __invalid_name__dat 
a { get; set; } 
    public int size { get; set; } 
    public string hash { get; set; } 
    public string title { get; set; } 
} 

public class Photo 
{ 
    public Content content { get; set; } 
} 

public class Details 
{ 
    public List<Address> address { get; set; } 
    public List<Name> name { get; set; } 
    public Gender gender { get; set; } 
    public string birthDate { get; set; } 
    public List<Identifier> identifier { get; set; } 
    public List<Telecom> telecom { get; set; } 
    public List<Photo> photo { get; set; } 
} 

public class EnrollmentSummary 
{ 
    public string dateEnrolled { get; set; } 
    public string __invalid_name__en 
roller { get; set; } 
} 

public class Self2 
{ 
    public string href { get; set; } 
} 

public class UnEnroll 
{ 
    public string href { get; set; } 
} 

public class PersonLink 
{ 
    public string href { get; set; } 
} 

public class PersonMatch 
{ 
    public string href { get; set; } 
} 

public class Links2 
{ 
    public Self2 self { get; set; } 
    public UnEnroll __invalid_name__un 
enroll { get; set; } 
    public PersonLink personLink { get; set; } 
    public PersonMatch personMatch { get; set; } 
} 

public class Person 
{ 
    public Details details { get; set; } 
    public bool __invalid_name__e 
nrolled { get; set; } 
    public EnrollmentSummary enrollmentSummary { get; set; } 
    public Links2 _links { get; set; } 
} 

public class Embedded 
{ 
    public List<Person> person { get; set; } 
} 

public class RootObject 
{ 
    public SearchParameters searchParameters { get; set; } 
    public string message { get; set; } 
    public LinKs __invalid_name___lin 
ks { get; set; } 
    public Embedded _embedded { get; set; } 
} 
+0

它适合你吗? – Gokul

+0

这工作..我错过了Rootobject。 – Gokul

+0

只需从json2csharp复制/粘贴即可。但是有很多冗余类,比如'Telecom(与* Identifier *相同)','SelfN','LinkN','PeriodN'和属性名称如'__invalid_name ___...'。 **非常差的答案**。 –

1

我想你的JSON字符串中有一些无效字符。通过验证器运行并添加必要的转义字符。

http://jsonlint.com/

+0

我复制到记事本并删除所有的endlines,然后复制到验证程序,它是有效的。 – DLeh

+0

JSON有效。我认为当OP张贴在JSON中时,Html编辑器添加了所有的结束语。 – harsimranb

0

下列功能就会JSON转换为C#类,其中T是类的类型。

public static T Deserialise<T>(string json) 
{ 
    T obj = Activator.CreateInstance<T>(); 
    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) 
    { 
     DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); 
       obj = (T)serializer.ReadObject(ms); // 
       return obj; 
    } 
} 

您需要System.Runtime.Serialization.json命名空间的引用。

该函数按以下方式调用;

calendarList = Deserialise<GoogleCalendarList>(calendarListString); 

calendarlist是C#类,calendarListString是包含JSON的字符串。