2016-08-23 1 views
0

Json 배열을 deserialize하려고하지만 작동하지 않습니다. I 출력 상자에 다음과 같은 메시지를 읽을 수 있습니다 :C#에서 Newtonsoft를 사용하여 JSON 배열을 비 직렬화

"Newtonsoft.Json.JsonReaderException : 잘못된 자바 스크립트 속성 식별자 문자 '. 경로'[0] ', 2 호선, 위치 37"

내 코드 : 당신의 도움에 대한

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Newtonsoft; 
using System.Collections.Generic; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

namespace JsonTest 
{ 
    [Activity(Label = "JsonTest", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button>(Resource.Id.MyButton); 

      button.Click += delegate 
      { 
       string json = @"[{ 
            id': '1', 
            'nom': 'zooz', 
            'prenom': 'Jack'         
           }, { 
            'id': '2', 
            'nom': 'toto', 
            'prenom': 'Blaireau'         
           }]"; 

       var a = JsonConvert.DeserializeObject<List<Person>>(json); 
       //JArray b = JArray.Parse(json); 


       Console.WriteLine(a); 
       // [email protected]}; 
      }; 
     } 

     public class Account 
     { 
      public string id { get; set; } 
      public string nom { get; set; } 
      public string prenom { get; set; } 
     } 

     public class Person 
     { 
      public Account person; 
     } 


    } 
} 

감사합니다.

+1

에 성공적으로 직렬화한다 ''1 '을 – hellowstone

+0

감사를. 그것은 어리석은 실수였다. 이 경우의 – Matthaousse

답변

2

단일 인용 수 Account

public class Person 
    { 
     public List<Account> person; 
    } 
0

귀하의 JSON은 작은 따옴표가 누락의 목록을 보유 할 수도

string json = @"[{ 
        'id': '1', 
        'nom': 'zooz', 
        'prenom': 'Jack'         
        }, 
        { 
        'id': '2', 
        'nom': 'toto', 
        'prenom': 'Blaireau'         
        }]"; 

모델 Person 필요 ID 누락, 그것은 또한 List<Account>에 해당 List<Person> 대신. 이 JSON은 목록 단일 인용 (') ID에 빠진 것 같다'

[{ 'person':{ 
    'id': '1', 
    'nom': 'zooz', 
    'prenom': 'Jack'         
}}, { 'person': { 
    'id': '2', 
    'nom': 'toto', 
    'prenom': 'Blaireau'         
}}] 
+0

에는 사람 목록을 보유하는 다른 모델 클래스가 필요합니다. – Mostafiz

+0

계정 클래스에는 JSON과 같은 id, nom 및 prenom 속성이 있습니다. person 클래스에는 person이라는 단일 속성이 있으며이 속성은 해당 계정 클래스의 인스턴스입니다. 그래서'List '으로 deserialize하기 위해서는 계정의 JSON 구조를 가진 person 속성을 추가해야합니다. 다른 옵션은'List '으로 비 직렬화하는 것입니다. 하지만 내가 볼 수있는 한 다른 모델 클래스에 대한 필요가 없습니다 ... – rmc00

+0

thats 사실,하지만 답변에서 귀하의 json 좀 봐 – Mostafiz

관련 문제