2017-10-15 1 views
0

Je classe csharp comme suit:Tapuscrit classe imbriquée, désérialisation JSON

public class StrategicPlan 
{ 
    [BsonId] 
    [BsonRepresentation(BsonType.ObjectId)] 
    public string Id { get; set; } 
    public string Unit { get; set; } 
    public List<Goal> Goals { get; set; } 

    public class Goal 
    { 
     public string GoalName { get; set; } 
     public int ImplementationRatio { get; set; } 
     public List<Target> Targets { get; set; } 

     public class Target 
     { 
      public string TargetName { get; set; } 
      public int ImplementationRatio { get; set; } 
     } 
    } 

    public DateTime UpdatedOn { get; set; } = DateTime.Now; 
    public DateTime CreatedOn { get; set; } = DateTime.Now; 
    public int UserId { get; set; } = 0; 
} 

J'ai une classe dactylographiée comme suit:

export class StrategicPlan { 
    constructor(jsonStr: string) { 
     let jsonObj: any = JSON.parse(jsonStr); 
     for (let prop in jsonObj) { 
      this[prop] = jsonObj[prop]; 
     } 
    } 
    Id: string; 
    Unit: string; 
    Goals: [ 
     { 
      GoalName: string, 
      ImplementationRatio: number, 
      Targets: [ 
       { 
        TargetName: string; 
        ImplementationRatio: number; 
       } 
      ] 
     }]; 
    UpdatedOn: Date; 
    CreatedOn: Date; 
    UserId: number; 
} 

Mes codes apicoles sont les suivants.

méthodes apicoles sont les suivantes:

public interface IRepository<T> 
    { 
     Task<IEnumerable<T>> GetAll(); 
     Task<T> Get(string id); 
     Task Add(T item); 
     Task<DeleteResult> Remove(string id); 
     //Task<UpdateResult> Update(string id, string body); 
     Task<UpdateResult> Update(string id, T item); 

     //// demo interface - full document update 
     //Task<ReplaceOneResult> UpdateDocument(ObjectId id, string body); 

     // should be used with high cautious, only in relation with demo setup 
     Task<DeleteResult> RemoveAll(); 
    } 

[Route("api/[controller]")] 
     public class StrategicPlansController : Controller 
     { 
      private readonly IRepository<StrategicPlan> _repository; 

      public StrategicPlansController(IRepository<StrategicPlan> repository) 
      { 
       _repository = repository; 
      } 

      // GET: api/StrategicPlan 
      [HttpGet] 
      public Task<string> Get() 
      { 
       return GetInternal(); 
      } 

      private async Task<string> GetInternal() 
      { 
       var items = await _repository.GetAll(); 
       return JsonConvert.SerializeObject(items); 
      } 
    } 

    public class StrategicPlanRepository : IRepository<StrategicPlan> 
    { 
     private readonly MongoContext _context = null; 

     public StrategicPlanRepository(IOptions<Settings> settings) 
     { 
      _context = new MongoContext(settings); 
     } 

     public async Task<IEnumerable<StrategicPlan>> GetAll() 
     { 
      try 
      { 
       return await _context.StrategicPlans.Find(_ => true).ToListAsync(); 
      } 
      catch (Exception ex) 
      { 
       // log or manage the exception 
       throw ex; 
      } 
     } 

}

public class MongoContext 
{ 
    private readonly IMongoDatabase _database = null; 

    public MongoContext(IOptions<Settings> settings) 
    { 
     var client = new MongoClient(settings.Value.ConnectionString); 
     if (client != null) 
      _database = client.GetDatabase(settings.Value.Database); 
    } 

    public IMongoCollection<StrategicPlan> StrategicPlans 
    { 
     get { return _database.GetCollection<StrategicPlan>("StrategicPlans"); } 
    } 
} 

J'ai un noyau asp.net application Web APIs au service des objets JSON.

Je ne peux pas obtenir ma classe imbriquée "Objectifs" et sa propriété "Cibles" de classe imbriquée est définie dans une application angulaire.

Il semble que le type d'objet. Je ne peux pas avoir de valeurs.

Comment puis-je l'analyser?

+1

Vous voulez dire, la propriété est définie non? Pouvez-vous s'il vous plaît fournir le code de la méthode API à partir de votre Web-API? – Nikolaus

+0

Oui, la propriété n'est pas définie. – Seyfullah

+0

J'ai ajouté des méthodes web api. – Seyfullah

Répondre

0

Comme je ne savais pas assez MongoDB, prochain essai: Peut-être une liste-Initializer pourrait aider:

public class StrategicPlan 
{ 
    [BsonId] 
    [BsonRepresentation(BsonType.ObjectId)] 
    public string Id { get; set; } 
    public string Unit { get; set; } 
    public List<Goal> Goals { get; set; } = new List<Goal>(); 

    public class Goal 
    { 
     public string GoalName { get; set; } 
     public int ImplementationRatio { get; set; } 
     public List<Target> Targets { get; set; } = new List<Target>(); 

     public class Target 
     { 
      public string TargetName { get; set; } 
      public int ImplementationRatio { get; set; } 
     } 
    } 

    public DateTime UpdatedOn { get; set; } = DateTime.Now; 
    public DateTime CreatedOn { get; set; } = DateTime.Now; 
    public int UserId { get; set; } = 0; 
} 
+0

J'utilise les méthodes db MongoDB.Driver. Il n'y a pas de méthode include sur la propriété StrategicPlans de type. var p = _context.StrategicPlans.Find (_ => true) .ToList(); remplace toutes les sous-entités de la classe de contrôleur. J'ai ajouté ma classe MongoContext dans ma question maintenant. – Seyfullah

+0

Après avoir changé ce que vous avez dit, j'obtiens la même réponse à http: // localhost: 5000/api/strategicplans. Le problème peut-il être dans la liaison de classe emboîtée angulaire que j'ai utilisée? – Seyfullah

+0

Pouvez-vous inspecter le corps de la requête et le corps de la réponse avec votre navigateur? Y a-t-il un objectif dans la base de données? – Nikolaus