Json MultiArray & ServiceStack

There is a line {"level": [{"level": 1, "points": 0, "name": "Some"}, {"level": 2, "points": 50, "name": "Second level "}, {" level ": 3," points ": 100," name ":" third level "}]}

How to fix the existing code or to add to get at the request when the exact same line?

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;
using System.Runtime.Serialization;  
using System.ComponentModel;  
using ServiceStack.ServiceHost;

namespace MSWA.Classes  
{  
  [DataContract]  
  [Description("Get config")]  
  [RestService("/gconf")]  
  public class GameConfiguration  
  {
    [DataMember]
    public string puid { get; set; }
  }

  [DataContract]
  public class GameConfigurationResponse
  {
    [DataMember]
    public LevelList ll;

    [DataMember]
    public string TestResponse { get; set; }
  }

  // Level List
  [DataContract]
  public class LevelList
  {
    [DataMember]
    public List<Level> level { get; set; }
  }

  // Desc one level
  [DataContract]
  public class Level
  {
    [DataMember]
    public int level { get; set; }
    [DataMember]
    public int points { get; set; }
    [DataMember]
    public string name { get; set; }
  }

  /// Create your Web Service implementation
  public class GameConfigurationService : IService<GameConfiguration>
  {
    public object Execute(GameConfiguration request)
    {
      // Original data
      string respValue = "";

      respValue = request.puid;

      if (respValue == null || respValue == "0") respValue = "0";

      Level lvl = new Level(){level=1, points=0, name=""};

      LevelList llist = new LevelList();
      llist.level.Add(lvl);

      return new GameConfigurationResponse
      {
          ll = llist
      };
    }
  }
}

Upvotes: 0

Views: 107

Answers (1)

Scott
Scott

Reputation: 21521

I hope I have understood your question. I think you are asking how to update your existing code so that it outputs this object:

{
    "level": [
        {"level": 1, "points": 0, "name": "Some"}, 
        {"level": 2, "points": 50, "name": "Second level"}, 
        {"level": 3, "points": 100, "name": "Third level"} 
     ]
 }

You should remove these lines:

Level lvl = new Level(){level=1, points=0, name=""};

LevelList llist = new LevelList();
llist.level.Add(lvl);

And replace with these:

LevelList llist = new LevelList();
llist.level = new List<Level>();
llist.level.Add(new Level { level = 1, points = 0, name = "Some" }); 
llist.level.Add(new Level { level = 2, points = 50, name = "Second level" }); 
llist.level.Add(new Level { level = 3, points = 100, name = "Third level" });

Update:

I presume from your comment you want to change GameConfigurationResponse to just output the List<Level> without having the LevelList object?

[DataContract]
public class GameConfigurationResponse
{
    [DataMember]
    public List<Level> level { get; set; } 
}

So the corresponding Execute method would be:

public object Execute(GameConfiguration request)
{
    // Original data
    string respValue = request.puid ?? "0";

    return new GameConfigurationResponse
    {
        level = new List<Level> {
            new Level { level = 1, points = 0, name = "Some" },
            new Level { level = 2, points = 50, name = "Second level" }, 
            new Level { level = 3, points = 100, name = "Third level" }
        }
    };
}

I am not sure what you are using respValue for. I have simplified it to string respValue = request.puid ?? "O"; That will set respValue to request.puid unless it is null, in which case it will be set to 0. But you aren't using this value, at least not in the code posted.

Upvotes: 1

Related Questions