Json MultiArray& ServiceStack

时间:2014-01-17 20:46:34

标签: c# json multidimensional-array servicestack

有一行{“level”:[{“level”:1,“points”:0,“name”:“Some”},{“level”:2,“points”:50,“name” “:”第二级“},{”级别“:3,”积分“:100,”名称“:”第三级“}}}

如何修复现有代码或添加以在完全相同的行时获取请求?

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
      };
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我希望我理解你的问题。我想您正在询问如何更新现有代码,以便输出此对象:

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

你应该删除这些行:

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

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

并替换为:

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" });

更新

我从您的评论中假设您要更改GameConfigurationResponse以仅输出List<Level>而没有LevelList对象?

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

因此相应的Execute方法将是:

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" }
        }
    };
}

我不确定您使用的是respValue。我已将其简化为string respValue = request.puid ?? "O";这将respValue设置为request.puid,除非它是null,在这种情况下它将被设置为0.但是您没有使用此值,至少不在发布的代码中。