Costas Aletrari
Costas Aletrari

Reputation: 387

How to set the object name on json

I'm returning JSON from ASP.NET Web API. the Json does return without a problem but at the beginning of the Json array there is no object name for me to use in an android application.

is there anyway to set it?

here is the Json that is being returned

THE OBJECT NAME SHOULD BE AT THE BEGINNING. NOT JUST STARTING WITH ID

[{"ID":1197,"CustomerGuid":"ea0af124-ab88-45d8-88f7-3f122ef53c04","UserTrackingID":"2624","EmailAddress":"[email protected]","Password":"Pakistan99","IsBusiness":null,"FullName":"Mashood","DateRegistered":"2014-11-26T18:51:47.977","PostCode":"44000","BusinessName":null,"FactualID":null,"FacebookToken":null,"Country":null},{"ID":1202,"CustomerGuid":"30c3781e-506c-490a-a27d-03a29696bdcd","UserTrackingID":"2643","EmailAddress":"[email protected]","Password":"george69","IsBusiness":null,"FullName":"Pending Customer","DateRegistered":"2014-11-27T08:08:04.29","PostCode":"4220","BusinessName":null,"FactualID":null,"FacebookToken":null,"Country":null},{"ID":1203,"CustomerGuid":"cf7d09e4-2e84-4b16-b38c-d32579d1c6c4","UserTrackingID":"2646","EmailAddress":"[email protected]","Password":"george69","IsBusiness":true,"FullName":"tester","DateRegistered":"2014-11-28T00:12:40.523","PostCode":"4102","BusinessName":"tester","FactualID":null,"FacebookToken":null,"Country":null}]


Public Class UsersController
    Inherits System.Web.Http.ApiController

    Private db As New PushStateNovember7Entities
    ' GET: api/Users
    Function GetUsers() As IQueryable(Of User)
        Return db.Users
    End Function

End Class

you can also view the project live at and access the helppage @

Upvotes: 0

Views: 1170

Answers (3)

Costas Aletrari
Costas Aletrari

Reputation: 387

hi all thankyou for your replies i've also found this post that fixed exactly my problem..

MVC4: Include object name in Json

Upvotes: 0

Aamir
Aamir

Reputation: 1757

Not sure why would u need a name at the beginning. If all u need is to parse the Json array in Android, you could do something like this:

Say you have the following User class:

public class User {
    private string _ID;
    private string _CustomerGuid;
    //and so on

    public string setID(string Id){ _ID = Id;} 
    public string getID(){ return _ID;}

    public string setCustomerGuid(string customerGuid){ _CustomerGuid = customerGuid;} 
    public string getCustomerGuid(){ return _CustomerGuid;}
}

To get/parse the Json array, you could do something like this:

JSONArray array = new JSONArray(yourApiReturnedJsonStringHere);
ArrayList<User> users = new ArrayList<User>();

for (int i=0; i < array.length(); i++) {
    User user = new User();

    JSONObject jObj = array.getJSONObject(i);

    user.setID(jObj.getString("ID"));
    user.setCustomerGuid(jObj.getString("CustomerGuid"));
    //.....

    users.add(user);
}

Upvotes: 1

Murtaza Tahir Ali
Murtaza Tahir Ali

Reputation: 609

You can try this

 var response = new ApiResponse<T>();
 response.Data = db.Users;
 return response;

This will encapsulate your list inside data name

If you are not using API controller then simply create Response class and

public Response<T> : where T : class
{
  public T Data {get; set;}
}

return new Response<IList<Users>>() { Data = db.Users};

Upvotes: 0

Related Questions