将多个JSON对象作为单个JSON对象传递给AngularJS

时间:2015-05-28 15:07:18

标签: ruby-on-rails json

我有一个有多个对象的函数。我想将这些对象作为一个JSON对象从我的rails应用程序传递到我的angularjs应用程序

@states = State.all
@nationalities = Nationality.all

    @states_nationalities = {
      states: @states,
      nationalities: @nationalities
    }

我以为我能做到这一点,但我收到了一个错误。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:0)

@states_nationalities = {
  states: @states,
  nationalities: @nationalities
}

当渲染为JSON时,上面的代码段应输出此结构(除非AR调用返回的数据不存在)。

{
  "states": [{}, {}, {}],
  "nationalities": [{}, {}, {}]
}

两个键都指向您应用中的状态和国籍的对象数组。

JSON结构的目​​标是什么?您遇到的错误是什么?

答案 1 :(得分:0)

<强> Hash.to_json

render :json  @states_nationalities

或用于渲染

    public Borrower(string firstname, string lastname, string address, string telno)
    {
        this._FirstName = firstname;
        this._LastName = lastname;
        this._Address = address;
        this._Telno = telno;
    }

    public Borrower() { }

    private int _CategoryId;

    private string _PersonId,
        _FirstName,
        _LastName,
        _Address,
        _Telno;

    public int CategoryId
    {
        get { return (int)this._CategoryId; }
        set { this._CategoryId = value; }
    }
    [Display(Name = "First name")]
    public string FirstName
    {
        get { return this._FirstName; }
        set { this._FirstName = value; }
    }
    [Display(Name = "Last name")]
    public string LastName
    {
        get { return this._LastName; }
        set { this._LastName = value; }
    }
    [Display(Name = "Person nr")]
    public string PersonId
    {
        get { return this._PersonId; }
        set { this._PersonId = value; }
    }
    [Display(Name = "Address")]
    public string Address
    {
        get { return this._Address; }
        set { this._Address = value; }
    }
    [Display(Name = "Telno")]
    public string Telno
    {
        get { return this._Telno; }
        set { this._Telno = value; }
    }

    public int save()
    {
        SqlConnection con = new SqlConnection(Settings.ConnectionString);
        SqlCommand cmd;
        int retVal = -1;
        bool existing;
        if (this._PersonId != null)
        {
            existing = false;
            cmd = new SqlCommand("INSERT INTO BORROWER (PersonId, FirstName, LastName, Address, Telno, CategoryId) VALUES ('" + this._PersonId + "','" + this._FirstName + "','" + this._LastName + "','" + this._Address + "','" + this._Telno + "'," + _CategoryId + ");", con);
        }
        else
        {
            existing = true;
            cmd = new SqlCommand("UPDATE BORROWER set FirstName='" + this.FirstName + "', LastName='" + this.LastName + "', Address='" + this.Address + "', Telno='" + this.Telno + "' WHERE PersonId='" + this.PersonId + "';", con);
        }
        try
        {
            con.Open();
            if (!existing)
            {

                retVal = cmd.ExecuteNonQuery();
            }
            else
            {
                retVal = -1;
                cmd.ExecuteScalar();
            }

        }
        catch (Exception er)
        {
            throw er;
        }
        finally
        {
            con.Close();
        }
        return retVal;
    }