I am fairly new to Azure search. While learning about it, I am wondering if it is possible to Convert the search result into a json formatted data(even if it is a complex one). So far, I have tried the following and was able to map it to a object.
parameters = new SearchParameters()
{
OrderBy = new[]{"companyName desc"},
Facets = new []{"companyName"}
};
using (var client = _azureSearchClientFactory.GetAzureIndexClientForAllAdverts())
{
client.Documents.Search<AdvertDetail>("Oslo", parameters);
}
If you want to covert the object to json string. As Yahnoosh mentioned that we could use the JsonConvert.SerializeObject(Object) to do that easily.
using Newtonsoft.Json
var json = JsonConvert.SerializeObject(object);
Related
I receive JSON output from several different web services. I need to obtain some token data from each service however it's in a different array each time. E.g.:
Service 1:
{
"service_name": "service1",
"service1_data": {
"token_data": "WSD123456789"
}
}
Service 2:
{
"service_name": "service2",
"service2_data": {
"token_data": "QSD76662345"
}
}
So I'm looking for a way to search for the value of "token_data" no matter where in the arrays it may be. At the moment I have to get it manually like so:
json1["service1_data"]["token_data"]
If theres a simple way to do this it would be much appreciated. Thanks!
You could convert the JSON to XML and then use an xpath like '//token_data' to find the token, assuming a simple string search or regex is not an option.
byte[] bytes = Encoding.ASCII.GetBytes(json);
using (var stream = new MemoryStream(bytes))
{
var quotas = new XmlDictionaryReaderQuotas();
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas);
var xml = XDocument.Load(jsonReader);
}
Xpath info can be found here
As suggested in the comments, you could use JSONPath.
In your scenario, $.service1_data.token_data.* should get you all the values you want.
This works only if the depth of token_data in the array is always the same.
The Newtonsoft.Json package - available on NuGet Install-Package Newtonsoft.Json allows you to walk Json objects using LINQ expressions.
var jsonText = #"{
""service_name"": ""service1"",
""service1_data"": {
""token_data"": ""WSD123456789""
}
}";
var jsonObject = JsonConvert.DeserializeObject<JToken>(jsonText);
System.Diagnostics.Debug.Assert(jsonObject["service1_data"].Value<string>("token_data") == "WSD123456789");
See the JToken reference to learn how to retrieve values from Json.
I'm trying to send an HttpRequest that takes a JSON object like this:
{
"some.setting.withperiods":"myvalue"
}
I've been creating anonymous objects for my other requests, but I can't do that with this one since the name contains a dot.
I know I can create a class and specify the [DataMember(Name="some.setting.withperiods")] attribute, but there must be a more lightweight solution.
There is no "easy" way to achieve this because the . in C# is reserved.
However, you could achieve something pretty close by using a dictionary and collection initializer. It's still somewhat isolated, and doesn't require you to create a custom class.
var obj = new Dictionary<string, object>
{
{ "some.setting.withperiods", "myvalue" }
};
var json = JsonConvert.SerializeObject(obj);
//{"some.setting.withperiods":"myvalue"}
You can use "JsonProperty" attribute for the same For example
[JsonProperty(".Name")]
public string Name { get; set; }
On top of this I want to add how to retrieve the data from the json where it has name property starting with special character as in Web API 2.0 token has ".issued".
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
var jsonRespons="json response from the web api";
var issue= JObject.Parse(jsonResponse).GetValue(".issued");
if you do it from in javascript, you can easily go back and forth, as shown with:
var obj = {
"some.setting.withdots":"myvalue"
};
var json = JSON.stringify(obj);
console.log(json);
var str = JSON.parse(json);
console.log(str);
have you tried putting it into a serialized string and sending that, then deserializing on the client-side?
you could do something like
var myAnnon = new
{
WithPeriod = "value"
};
var j = JsonConvert.SerializeObject(myAnnon);
j = j.Replace("WithPeriod", "some.setting.withdots");
You can use a JObject (part of Json.Net's LINQ-to-JSON API) to create the JSON in question:
string json = new JObject(new JProperty("some.setting.withperiods", "myvalue")).ToString();
Fiddle: https://dotnetfiddle.net/bhgTta
You could try prefixing your member names with a # to allow use of literals, but the way to do it is using [DataMember] as you have already mentioned in your question.
I am trying to make a windows 8 Store app that gets results from a MySQL database from a PHP page as a REST service.
I'm looking for the PHP to return a JSON representation of an array of strings and have done that happily when dong the same between Javascript and PHP.
I need to take that same JSON string and use it in my C# Windows 8 store App, is there a way to take the return of that PHP page and convert it into a normal C# array, not a dictionary or more complex collection.
The database does have four fields so if i have to use a special object made for this i will, but I'd rather I didn't as this function doesn't require that amount of data.
The PHP page is like so - $search_text is passed in via a GET:
$databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($databaseConnection->connect_error)
{
echo "Database connection failed: $databaseConnection->connect_error";
}
else
{
$search_text = $search_text."%";
$query = "SELECT DISTINCT street FROM gritroutes WHERE street LIKE ? LIMIT 5";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('s', $search_text);
$statement->execute();
$statement->store_result();
$statement->bind_result($street);
$autonumber = 1;
while ($statement->fetch())
{
$resultarr[] = $street;
}
$statement->close();
echo json_encode($resultarr);
}
Just to be clear. I am writing a Windows Store App, the System.Web Namespace is unavailable so i can't use JavaScriptSerializer.
Just to add to Matthew's answer, you can deserialize using Json.NET (you can get it from NuGet), you'd do something like:
List<string> myStrings = JsonConvert.DeserializeObject<List<string>>(myJson);
This is in:
using Newtonsoft.Json;
Check out this article for practical example.
EDIT
- I'd also like to throw in this link, since it's just awesome.
I hope this helps.
Take a look at the JavaScriptSerializer class.
string myJson = "{blablabla I'm json}";
var serializer = new JavaScriptSerializer();
var myStrings = serializer.Deserialize<List<string>>(myJson);
foreach (var str in myString)
{
Console.WriteLine(str);
}
You could also use native Windows.Data.Json classes to do the parsing:
string json = #"[""item1"", ""item2"", ""item3""]";
var array = JsonArray.Parse(json).Select(i => i.GetString()).ToArray();
I want to use JavaScriptSerializer to send a package of JSON data that contains both a list of objects as well as a string, identified like ChatLogPath. As far as I can tell, that class can only serialize one object -- being the list -- and if I try to append multiple ones it obviously just creates invalid JSON like {...}{...} which won't work.
Is there any way to do this? I'm insanely new to C# and ASP.NET MVC so forgive me if this is a dumb question :)
Edit: here's my code as of right now.
string chatLogPath = "path_to_a_text_file.txt";
IEnumerable<ChatMessage> q = ...
...
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(q) + json.Serialize(chatLogPath);
Which will output the array like this in JSON { ... } followed by the chatLogPath { ... }. In other words, it can't work since that's invalid JSON.
The easiest way to get a single JSON object with the array and path together is to create a class or dynamic object with each as a property/field of it.
Class example:
public class ChatInformation {
public IEnumerable<ChatMessage> messages;
public string chatLogPath;
}
...
var output = new ChatInformation {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);
Dynamic example (requires .NET 4+):
dynamic output = new ExpandoObject {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);
Anonymous Type example (if you don't care to have another class, nor are you on .NET 4):
var output = new {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);
I started looking around for a search engine and after some reading I decided going with ElasticSearch (which is quite amazing :)), my project is in C# so I looked around for a client and started using NEST, everything is quite straightforward but I am a bit confused on the searching part.
I want to search all fields on a specific type what I came up with is the following code:
elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));
I saw that much of the string query search is deprecated and wanted to make sure the above is the correct way of doing this (the above is not marked as deprecated...) also it is a bit long for a simple task so maybe anyone knows another way of doing this.
Thanks
I just use the string query version: create my query object using C# anonymous type and serialize it to JSON.
That way, I can have straightforward mapping from all the JSON query examples out there, no need translating into this "query DSL".
Elasticsearch by itself evolves quite rapidly, and this query DSL thus is bound to lack some features.
Edit: Example:
var query = "blabla";
var q = new
{
query = new
{
text = new
{
_all= query
}
},
from = (page-1)*pageSize,
size=pageSize
};
var qJson = JsonConvert.SerializeObject(q);
var hits = _elasticClient.Search<SearchItem>(qJson);
Just to confirm
elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));
Is the preferred way to search and the fact it feels a bit long is because there are alot of options you can play with that are not used here. I'm always open on suggestions to make it shorter!
The string overload is deprecated but wont be removed from NEST. I'll update the obsolete message to explicitly mention this.
If the anonymous types above aren't your thing, you can just use JObjects from json.net and build your query that way. Then you can run it the same way above.
JObject query = new JObject();
query["query"] = new JObject();
query["query"]["text"] = new JObject();
query["query"]["text"]["_all"] = searchTerm;
query["from"] = start;
query["size"] = maxResults;
string stringQuery = JsonConvert.SerializeObject(query);
var results = connectedClient.SearchRaw<SearchItem>(stringQuery);
I like this way better because the DSL in ES uses reserved keywords in C#, like bool, so I don't have to do any escaping.
With ElasticSearch 2.0, I have to use a SearchResponse< NewType > in the Search method like this :
var json = JsonConvert.SerializeObject(searchQuery);
var body = new PostData<object>(json);
var res = _elasticClient.Search<SearchResponse<NewType>>(body);
IEnumerable<NewType> result = res.Body.Hits.Select(h => h.Source).ToList();
Hope it help.
Note: I found very few documentation about PostData class and its generic type parameter