I am trying to read a json file and parse it using
jArray = JArray.Parse(json);
And filtering that array based on some conditions and returning same in ActionREsult OK
var categories = jArray.SelectTokens("[*]")
.Where(token => (string)token.SelectToken(jPath) == displayLevel);
return Ok(categories);
But on client side I am receiving it as json string with empty array like this
[[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]],[[[]],[[]],[[]],[[]],[[]]]]
All this was working before.
I have a simple WinForm program that does a WebRequest to a server, the response is a string looking like:
{"a":"foo","b":"bar"}
How do convert this string to a dictionary? (Dictionary<string, string>). Is there a built-in function I can use?
Use a library like JSON.NET:
var dictionary = JsonConvert.DeserializeObject<IDictionary<string, string>>(json)
var sourve = #"{"a":"foo","b":"bar"}";
Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(source);
If you can add two assemblies:
System.Web
System.Web.Extensions
You can use this code:
string html = "{\"a\":\"foo\",\"b\":\"bar\"}";
JavaScriptSerializer jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, string>>(html);
I get data from instagram api. So i use below
var responseStream = webRequest.GetResponse().GetResponseStream();
Encoding encode = System.Text.Encoding.Default;
using (StreamReader reader = new StreamReader(responseStream, encode))
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = serializer.DeserializeObject(reader.ReadToEnd());
}
I dont know how to loop on this data. I tried
foreach (var item in jsonObject)
{
}
But it gives compile time error Type object is not enumerable
How do i loop?
Your variable jsonObject is an object which is not an enumerable.
You must specify an enumerable type when deserializing :
var jsonObject = serialize.Deserialize<enumerableType>(reader.ReadToEnd());
I'm trying to insert a row (with nested records) into BigQuery using the C# API. I'm able to insert a row (w/ nested records) using the JavaScript API. But using the C# API i'm getting error saying: "Repeated field must be imported as a JSON Array". He is a simple row that I was able to insert using the JavaScript API
var json = {'rows':[{'json':
{"inputs" : [{
"Age":"10"
}]}}]};
This works fine in JS, but I'm unclear how to do this in C#.
Here is my attempt:
var r = new TableDataInsertAllRequest.RowsData();
r.Json = new Dictionary<string, object>();
var dict = new Dictionary<string, object>();
dict.Add("Age", "10");
r.Json.Add("inputs", dict);
Also I tried using JSON API
string json = JsonConvert.SerializeObject(input, jsonSettings);
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, jsonSettings);
r.Json.Add("jsonInputs", dict);
Here is the API Doc for RowsData https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/csharp/latest/classGoogle_1_1Apis_1_1Bigquery_1_1v2_1_1Data_1_1TableDataInsertAllRequest_1_1RowsData.html
Its somewhat vague or unclear how to do nested records. I tried just writing straight JSON but getting the same errors.
Any help would be much appreciated.
Thanks,
From the json example that works there are two arrays. I don't see any arrays in either of two failing examples you provide.
I suspect adding the arrays will fix your problem. Consider parsing the known-working json string into a json object and using that as a quick test.
The following code snippet successfully works for bigquery:
JArray inputs = new JArray();
JObject inputOne = new JObject(new JProperty("Age", "12"));
inputOne.Add(new JProperty("BirthDate", "1234"));
r.Json.Add("inputs", inputs);
I am using razor in webmatrix.
I have a dictionary object
var occupancyTotalDic = new Dictionary<double, int>();
Once I have filled the dictionary with data I wish to output as JSON as a response to populate a flot chart
Dictionary<String, String> input = new Dictionary<String, String>();
occupancyTotalDic = input.ToDictionary(item => item.Key, item => (String)item.Value);
Response.ContentType = "application/json";
Response.Write(Json.Encode(occupancyTotalDic));
How do I convert my dictionary to contain strings so that I can output as JSON?
Since Webmatrix can't compile MVC application I assume you are using ASP.NET webforms.
Tried this solution https://stackoverflow.com/a/5597628/713789
If you are using MVC then just use JsonResult test(){ return Json(myobj);}