2017-01-19 7 views
1

Newtonsoft.Json을 사용하여 json을 비 직렬화합니다. 여기 내 json 문자열입니다.json 객체를 문자열로 참조 할 때 RuntimeBinderExeption

{ 
    "data": { 
     "type": "records", 
     "id": "7", 
     "attributes": { 
      "created": "2017-01-19T08:42:56Z", 
      "updated": "2017-01-19T08:42:56Z", 
      "state": 3, 
      "data": { 
       "Lastname": [ 
        "Gonzales" 
       ], 
       "Firstname": [ 
        "Lacy" 
       ], 
       "Email": [ 
        "[email protected]" 
       ], 
       "Salutation": [ 
        "Mrs." 
       ] 
      } 
     } 
    } 
} 

그래서 이와 같은 동적 변수를 만들고 firstname을 문자열 s에 할당하면 RuntimebinderException이 발생합니다.

dynamic data = JsonConvert.DeserializeObject(responseString2); 
string s = data.attributes.data.Firstname; 

내가 잊어 버린 것이 있습니까? 귀하의 도움에 감사드립니다

답변

3

JSON 구조로 판단하면 Firstname은 실제로 하나의 항목이있는 배열입니다.

또한 JSON에 data이라는 루트 객체가 포함되어 있으므로 변수가 누락 된 것 같습니다 (변수 이름을 data으로 지정 했으므로 혼란 스러울 수도 있음). 또한,

"FirstName": "Lacy", 

: 반환 된 JSON의 제어에 있다면 분명히

dynamic result = JsonConvert.DeserializeObject(responseString2); 
string s = result.data.attributes.data.Firstname[0]; 

는, 더 쉽게 소비 뭔가를 리팩토링 수

내가 함께 갈 것입니다 원시를 사용하여 데이터에 액세스하여 dynamic을 모두 피할 수 있습니다.

var firstName = JObject.Parse(json).SelectToken("data.attributes.data.Firstname[0]").ToString(); 

0

json을 바꿀 수있는 방법이 있으면 아래에서와 같이 이름에서 그 []를 제거하는 등의 작업을 할 수 있습니다. 그런 다음 값에 액세스 할 수 있습니다.

"data": { 
    "type": "records", 
    "id": "7", 
    "attributes": { 
     "created": "2017-01-19T08:42:56Z", 
     "updated": "2017-01-19T08:42:56Z", 
     "state": 3, 
     "data": { 
     "Lastname":"Gonzales", 
     "Firstname":"Lacy", 
     "Email":"[email protected]", 
     "Salutation":"Mrs." 
     } 
    } 
    } 
} 

하거나 @ haim770 그의 대답에 언급처럼 json을 변경하지 않고 액세스 할 수 있습니다.

1

우선, 귀하의 json은 잘못된 것 같습니다.

올바른 JSON :

{ 
"data": { 
    "type": "records", 
    "id": "7", 
    "attributes": { 
     "created": "2017-01-19T08:42:56Z", 
     "updated": "2017-01-19T08:42:56Z", 
     "state": 3, 
     "data": { 
      "Lastname": [ 
       "Gonzales" 
      ], 
      "Firstname": [ 
       "Lacy" 
      ], 
      "Email": [ 
       "[email protected]" 
      ], 
      "Salutation": [ 
       "Mrs." 
      ] 
     } 
    } 
} 
} 

FIRSTNAME는 List<string>하지 string입니다.

List<string> ls = obj.data.attributes.data.Firstname; 

Data2는로 직렬화되기 때문에.

public class Data2 
    { 
     public List<string> Lastname { get; set; } 
     public List<string> Firstname { get; set; } 
     public List<string> Email { get; set; } 
     public List<string> Salutation { get; set; } 
    } 

하나 더 제안.

Data2가 하나의 persion에만 속하면, json 구조를 변경해야한다고 생각합니다.

{ 
"data": { 
    "type": "records", 
    "id": "7", 
    "attributes": { 
     "created": "2017-01-19T08:42:56Z", 
     "updated": "2017-01-19T08:42:56Z", 
     "state": 3, 
     "data": { 
      "Lastname": "Gonzales", 
      "Firstname": "Lacy", 
      "Email": "[email protected]", 
      "Salutation": "Mrs." 

     } 
    } 
} 
} 

그리고 클래스를 비 직렬화하는 경우. Data2 클래스는 이렇게됩니다.

public class Data2 
    { 
     public string Lastname { get; set; } 
     public string Firstname { get; set; } 
     public string Email { get; set; } 
     public string Salutation { get; set; } 
    } 

그리고 나서 현재 무엇을하고 있습니까?

string firstName = obj.data.attributes.data.Firstname;