How to extract List from HttpResponseMessage in .Net - list

I have a method which returns me HttpResponseMessage as below,
HttpResponseMessage allOrders= PerformGet(null);
Now i want to extract list of content from allOrders how to do it in .Net.
i want to get a list of content from HttpResponseMessage.

Related

Regex For Work Items in Team Services API

I'm retrieving a list of Work Items using the VSTS API and would like to display them on my web app. I can successfully return a list of the work items in the format below:
{"count":1,"value":[{"id":246,"rev":4,"fields":{"System.Id":246,"System.State":"New","System.Title":"test1"},"url":"https://example.visualstudio.com/_apis/wit/workItems/246"}]}
I have tried a regular expression to get the values from this HTTP response with the following code:
HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.asOf + "&api-version=2.2").Result;
if (getWorkItemsHttpResponse.IsSuccessStatusCode)
{
result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
// Regular expression to extract work item values to display
string parseWI = result.ToString();
var match = Regex.Match(parseWI, "\"System.ID\": (.*)");
workItemsToDisplay = (match.Groups[1].Value);
}
}
}
}
return workItemsToDisplay;
}
This is refusing to return anything though and leaves the textbox I display the workItemsToDisplay in empty. I'm not familiar with regular expressions and i'm sure this is where the issue stems from. Not sure if Microsoft already has sample code to construct a display of Work Items from the response.
Don't use a regex. That's JSON, use a JSON parsing library (JSON.Net is the de facto standard in the .NET world) and then you can easily retrieve specific fields.

Webservice(asmx) returns array, not list

I have a deserialization webmethod which returns a list of data in webservice(asmx), and I am calling the method from client-side. However, the method is giving me an array, not a list. I understand that it is because of SOAP response which returns xml format (or something like that..)
Is it possible to return a list? If then, please tell me an idea. If not, please teach me an alternative way. (I should not use array...)
service.asmx.cs
[WebMethod]
public IList<Person> DeserializeJson(string value)
{
JavaScriptSerializer js = new JavaScriptSerializer();
IList<Person> tableData = js.Deserialize<IList<Person>>(value);
return tableData;
}
Client.aspx.cs (WebService is my service reference)
WebService.Service1SoapClient client = new WebService.Service1SoapClient();
string stream = client.CreateJsonFromDatabase();
List<WebService.Person> tableData = client.DeserializeJson(stream);
Web services do not return arrays, and they do not return lists. They return XML. The XML they return is interpreted by the client code as a list, array, or whatever.
If you consume this service by using "Add Service Reference", then you will have a choice of how to treat repeated elements in the XML. You can choose from List, Array, or several other choices.

web servise & data binding with WP7

I want to display a list of friends, and when i select a friend my app will navigate to another page showing this informations related to this friend.
I'm trying to read data using web service and display some of it(name and photo) on a costumized lisBox, and store some (id) temporarily in a list or collection that i can call it after and use it in my url:
NavigationService.Navigate(new Uri("/MyApp;component/FriendDetails.xaml?id{0}",friend_id, UriKind.Relative));
Use WebService to query the api and you need to add a "download callback" in that callback use linq to write the result of the query to an observable collection of an object matching the data you want from the result.
like this.
friends = new ObservableCollection<Friend>();
WebClient wc = new WebClient();
wc.OpenReadCompleted += Feed;
wc.OpenReadAsync(new Uri(friendsURL));
}
private void Feed(object Sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null){
return;
}
using (Stream s = e.result){
XDocument doc = XDocument.Load(s);
then use Linq to cycle through the data and add it your observablecollection of friends.

Struts 2 List Parameter Passing

How do I pass a list of integers using the s:a href and param tags in Struts 2?
Example POJO:
private List<Integer> myList = new ArrayList<Integer>();
public List<Integer> getMyList() {
return myList;
}
public void setMyList(List<Integer> myList) {
this.myList = myList;
}
Example JSP Page:
<s:url id="testUrl" action="testAction">
<s:param name="myList" value="%{myList}" />
</s:url>
<s:a href="%{testUrl}">Test Link</s:a>
When I click on the "Test Link", the form submits the following for myList:
[1,+2,+3,+4,+5]
This results in Struts re-directing to the "input" page. This is not the desired behavior. Does anyone have any suggestions about how to pass a list of integers correctly using the Struts tags?
The param tag calls the toString on the list to put the parameter in the URL if I remember right. Therefore the action that should get the list only gets a string.
The setter on the next action needs to accept a string and in this setter you could split up the string, extract the numbers and fill a new list with that.

How do I use RegEx to insert into a JSON response?

I'm using JSON for a web application I'm developing. But for various reasons I need to create "objects" that are already defined on the client script based on the JSON response of a service call. For this I would like to use a regex expression in order to insert the "new" statements into the JSON response.
function Customer(cust)
{
this.Name = null;
this.ReferencedBy = null;
this.Address = null;
if (cust != null)
{
this.Name = cust.Name;
this.ReferencedBy = cust.ReferencedBy;
this.Address = cust.Address;
}
}
The JSON response is returned by an ASP.NET AJAX Service and it contains a "__type" member that could be used to determine the object type and insert the "new" statement.
Sample JSON:
{"__type":"Customer", "ReferencedBy":{"__type":"Customer", "Name":"Rita"}, "Name":"Joseph", "Address":"123 {drive}"}
The resulting string would look like this:
new Customer({"ReferencedBy":new Customer({"Name":"Rita"}), "Name":Joseph", "Address":"123 {drive}"})
I got this so far but it doesn't work right with the ReferencedBy member.
match:
({"__type":"Customer",)(.*?})
replace:
new Customer({$2})
Hmmm why don't you try to make a simplier way to do it? e.g.:
var myJSON = {"__type":"Customer", "ReferencedBy":{"__type":"Customer", "Name":"Rita"}, "Name":"Joseph", "Address":"123 {drive}"};
after check the type: myJSON.__type, and if it is customer, then:
new Customer({"ReferencedBy":new Customer({"Name":myJSON.ReferencedBy.Name}), "Name":myJSON.Name, "Address":myJSON.Address });
It is because you already have a defined data structure, it is not neccessary to use regex to match pattern & extract data.