Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
CSS
I need this as my json return. This data is being pulled from the DB but currently I am using static data.

 {
        "data": [
            {
                "id": 1,
                "latitude":17.3700,
                "longitude": 78.4800,
                "gallery":
                    [
                        "assets/img/items/1.jpg"
                    ]
            }
        ]
    }
I have tried this in my code behind but I am not getting the desired result.

[WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public List<account>; getData()
    {
        Account account = new Account
        {
            id = 1,
            latitude = &quot;17.3700&quot;,
            longitude =&quot;78.4800&quot;,
            gallery = new List&lt;string&gt;
                  {
                    assets/img/items/1.jpg&quot;,
                     assets/img/items/2.jpg&quot;,
                  }
        };
        return new JavaScriptSerializer().Serialize(account);
    }

    public class Account
    {
        public int id { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public IList<> gallery  { get; set; }
    }
Result:

{
 "id":2,
 "latitude":"17.3700",
 "longitude":"78.4800",
 "gallery":["assets/img/items/1.jpg","assets/img/items/2.jpg"]
}</account>
Posted

1 solution

not sure if Im seeing this correctly

1)
public List<account>; getData()
(look at the semi-colon ';')

2) Im not sure if I like the entire way this is set up ..

public List<account>; getData()
    {
        Account account = new Account
        {
            id = 1,
            latitude = &quot;17.3700&quot;,
            longitude =&quot;78.4800&quot;,
            gallery = new List&lt;string&gt;
                  {
                    assets/img/items/1.jpg&quot;,
                     assets/img/items/2.jpg&quot;,
                  }
        };
        return new JavaScriptSerializer().Serialize(account);
    }


I think what you want to be doing is something more like

C#
List<account> Accounts = new List<account>();
    // Create new Account
    // Accounts.Add(Account)
    // then comes the return JavaScriptSerializer().Serialize(Accounts);
</account></account>

It looks like in your top example you are expecting a list of data, whereas you're only generating a single datum and serialising that

I could be wrong .....

[edit] whoops, Ive blown the formatting up .. sorry, hope you still get the idea[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900