so i'm getting a json file from the web into my program that reads something like this:
{
"10": {
"appid": 10,
"name": "Counter-Strike",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 183964,
"negative": 4782,
"userscore": 0,
"owners": "10,000,000 .. 20,000,000",
"average_forever": 11228,
"average_2weeks": 289,
"median_forever": 210,
"median_2weeks": 114,
"price": "999",
"initialprice": "999",
"discount": "0",
"ccu": 13567
},
"20": {
"appid": 20,
"name": "Team Fortress Classic",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 5223,
"negative": 871,
"userscore": 0,
"owners": "2,000,000 .. 5,000,000",
"average_forever": 522,
"average_2weeks": 0,
"median_forever": 20,
"median_2weeks": 0,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 93
},
"30": {
"appid": 30,
"name": "Day of Defeat",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 4866,
"negative": 543,
"userscore": 0,
"owners": "5,000,000 .. 10,000,000",
"average_forever": 2191,
"average_2weeks": 343,
"median_forever": 24,
"median_2weeks": 343,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 130
},
"40": {
"appid": 40,
"name": "Deathmatch Classic",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 1789,
"negative": 400,
"userscore": 0,
"owners": "5,000,000 .. 10,000,000",
"average_forever": 297,
"average_2weeks": 0,
"median_forever": 8,
"median_2weeks": 0,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 6
}
I'm importing it has a string how can I get an enumerable or list where I get all the tokens as (Jtokens) so "IEnumerable<JToken> or List<JToken>" like ["10", "40", "60"...]
This is what my code looks like right now:
string json = webClient.DownloadString("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
tokens = JObject.Parse(json).Children();
//token = JObject.Parse(json).SelectToken("applist.apps");
for (int i = 0; i < tokens.Count(); i++)
{
int currentID = (int)tokens.ElementAt(i).SelectToken("appid");
if (SteamApps.BIsSubscribedApp(new AppId_t((uint)currentID)))
{
threads.Add(new Thread(new ParameterizedThreadStart(AddToDictionary)));
threads.Last().Start(new stats(i, currentID, threads.Last()));
}
}
But this isn't working at all and I can't read any values..
There are oodles of ways to do this and variations. However, this is simple enough. The premise is Parse -> Select First -> Target property by name
var results = JToken.Parse(input)
// Select past the dictionary
.Select(x => x.First())
// project property values in to a value tuple
.Select(x => (Appid: x["appid"], Name: x["name"]))
.ToArray();
foreach (var item in results)
Console.WriteLine(item);
Output
(10, Counter-Strike)
(20, Team Fortress Classic)
(30, Day of Defeat)
(40, Deathmatch Classic)
by selecting the token u want from the list of objects that was deserialised from the json string using system.linq
newlist = deserList.Select(x => x.Jtoken);
I have a JSON data like bellow
[{
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":2,\"Total\":\"4000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":5,\"Total\":\"2500.00\",\"IsNote\":true}]"
}]
and I want to extract value from it and expect bellow data
[{
"PK_MasterCurrencyDenomID": 1,
"CurrencyDenomination": 2000,
"NoofCurrency": 2,
"Total": "4000.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 2,
"CurrencyDenomination": 500,
"NoofCurrency": 2,
"Total": "1000.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 3,
"CurrencyDenomination": 200,
"NoofCurrency": 2,
"Total": "400.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 4,
"CurrencyDenomination": 100,
"NoofCurrency": 2,
"Total": "200.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 5,
"CurrencyDenomination": 50,
"NoofCurrency": 2,
"Total": "100.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 6,
"CurrencyDenomination": 20,
"NoofCurrency": 2,
"Total": "40.00",
"IsNote": true
}]
to do that I write bellow code ,and think this is not right way to do that there must be some smart way to do that .Please suggest me a better alternative.
JArray jsonArray = JArray.Parse(json);
List<MasterCurrencyDenomination> d = new List<MasterCurrencyDenomination>();
string strjson = string.Empty;
foreach (var item in jsonArray.Children())
{
strjson+= item["CurrencyDenomination_JSON"].ToString().Replace("[", "").Replace("]", ",");
}
d = JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>("["+strjson+"]");
If you observe the Json, it is evident that it is an Array of Type which contains a single String Property CurrencyDenomination_JSON. The Value of CurrencyDenomination_JSON is a JSON string.
What you need to do is, fetch these JSON strings (represented as IEnumerable<string>), retrieve JObject from them by parsing each of them and Serialize the collection.
var currencyArray = JArray.Parse(json).Children<JObject>()
.SelectMany(x=>x.Properties().Select(c=>c.Value.Value<string>()));
var result = JsonConvert.SerializeObject(currencyArray.SelectMany(x=>JArray.Parse(x)));
I think You can use the following code to do this.
List<MasterCurrencyDenomination> items = new List<MasterCurrencyDenomination>();
JToken.Parse(myJson).ToList().ForEach(x =>
{
items.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(x.SelectToken("CurrencyDenomination_JSON").Value<string>()));
});
OR:
List<MasterCurrencyDenomination> items2 = new List<MasterCurrencyDenomination>();
foreach (var test in JToken.Parse(myJson))
{
items2.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(test.SelectToken("CurrencyDenomination_JSON").Value<string>()));
}
I'm trying to setup an integration point to an Automatic License Plate Recognition (ALPR) system. I'm using OpenALPR.
On the local PC there is an agent with a IP camera installed that will read the license plate and send info to the cloud. In the cloud there is a WebHook integration point that will fire each time a license plate is registered.
In the OpenALPR webpage the WebHook documentations says:
WebHooks send an HTTP POST to your URL every time a plate is received or an alert is triggered. The POST body contains the license plate data.
I've tested the integration by sending the output to a WebHook test page found on this URL: https://webhook.site/
The result is shown at the end of this post. As can be seen the output is JSON format.
I've done a RESTful service and deployed it in Azure. My problem is that when I read the result, I don't get the full JSON result. Based on the test on the http://webhook.site, I know the WebHook provider (sender) sends all JSON data so I can't figure out why it's not fully received.
If there is a better way to receive the JSON result in my web app I'm happy to hear about it. Below is my take on how to get the job done. Except it does not work ! :-D
Can anyone tell me what I'm doing wrong and why only part of the result is read?
This is the part of the code that is called on a HTTP POST to my service.
// POST: api/OpenALP
public HttpResponseMessage Post([FromBody]string value)
{
var content = Request.Content;
Request.Content.LoadIntoBufferAsync().Wait();
string jsonContent = content.ReadAsStringAsync().Result;
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
EDIT: Don't know if it's important- but the debugging was done by publishing the service as "Debug" and the attaching a debugger. The content shown below was copied out from the break point.
This is what I get in string jsonContent
" \"color\": [{\"confidence\": 92.68022918701172, \"name\": \"white\"}, {\"confidence\": 2.699702501296997, \"name\": \"silver-gray\"}, {\"confidence\": 1.8385039567947388, \"name\": \"yellow\"}, {\"confidence\": 1.024902582168579, \"name\": \"gold-beige\"}, {\"confidence\": 0.5838126540184021, \"name\": \"green\"}], \"make\": [{\"confidence\": 16.192890167236328, \"name\": \"renault\"}, {\"confidence\": 12.871005058288574, \"name\": \"mercedes-benz\"}, {\"confidence\": 7.908616065979004, \"name\": \"volkswagen\"}, {\"confidence\": 4.901283264160156, \"name\": \"opel\"}, {\"confidence\": 4.583385467529297, \"name\": \"bmw\"}], \"body_type\": [{\"confidence\": 20.72427749633789, \"name\": \"sedan-standard\"}, {\"confidence\": 18.412166595458984, \"name\": \"van-mini\"}, {\"confidence\": 17.614177703857422, \"name\": \"sedan-compact\"}, {\"confidence\": 12.435933113098145, \"name\": \"sedan-wagon\"}, {\"confidence\": 6.872223377227783, \"name\": \"suv-crossover\"}], \"year\": [{\"confidence\": 23.91012191772461, \"name\": \"2005-2009\"}, {\"confidence\": 19.78921890258789, \"name\": \"2000-2004\"}, {\"confidence\": 16.811452865600586, \"name\": \"1995-1999\"}, {\"confidence\": 11.53968620300293, \"name\": \"2010-2014\"}, {\"confidence\": 8.91786003112793, \"name\": \"1985-1989\"}], \"make_model\": [{\"confidence\": 2.5598113536834717, \"name\": \"renault_kangoo\"}, {\"confidence\": 1.564223289489746, \"name\": \"fiat_doblo\"}, {\"confidence\": 1.5174164772033691, \"name\": \"alfa-romeo_147\"}, {\"confidence\": 1.0350353717803955, \"name\": \"citroen_2cv\"}, {\"confidence\": 0.986167311668396, \"name\": \"volkswagen_caddy\"}]}, \"best_uuid\": \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355349\", \"epoch_end\": 1521329358227, \"best_image_width\": 640, \"data_type\": \"alpr_group\", \"best_image_height\": 480, \"frame_end\": 865634, \"is_parked\": false, \"web_server_config\": {\"agent_label\": \"DESKTOP-B3PMA1C\", \"camera_label\": \"WebCamHKA\"}, \"best_region\": \"eu-dk\", \"uuids\": [\"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355147\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355183\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355248\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355284\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355349\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355386\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355451\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355523\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355558\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355700\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355777\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355815\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355889\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356029\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356274\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356421\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356598\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356738\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356773\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356874\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356909\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357014\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357049\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357148\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357387\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357422\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357567\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357631\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357734\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357870\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357976\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358012\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358048\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358084\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358155\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358191\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358227\"], \"plate_indexes\": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"travel_direction\": 255.1732177734375, \"country\": \"eu\", \"best_plate_number\": \"AF22454\", \"best_region_confidence\": 91.45714569091797, \"agent_version\": \"2.5.103\", \"candidates\": [{\"matches_template\": 0, \"plate\": \"AF22454\", \"confidence\": 94.99976348876953}, {\"matches_template\": 0, \"plate\": \"AF2245\", \"confidence\": 69.99081420898438}, {\"matches_template\": 0, \"plate\": \"AF224\", \"confidence\": 68.92207336425781}, {\"matches_template\": 0, \"plate\": \"A22454\", \"confidence\": 65.7693099975586}, {\"matches_template\": 0, \"plate\": \"AY22454\", \"confidence\": 65.72945404052734}, {\"matches_template\": 0, \"plate\": \"AT22454\", \"confidence\": 65.69474029541016}]}"
If I look at the output on the WebHook test site (http://webhoot.site) I get this result:
{
"epoch_start": 1521286407536,
"camera_id": 645063384,
"frame_start": 109416,
"agent_uid": "WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X",
"best_confidence": 94.99918365478516,
"company_id": "d1806fef-5549-4915-86a8-1b1f975e8df9",
"version": 2,
"agent_type": "alprd",
"best_plate": {
"plate": "AF25463",
"confidence": 94.99918365478516,
"region_confidence": 99,
"vehicle_region": {
"y": 1,
"x": 80,
"height": 479,
"width": 479
},
"region": "eu-dk",
"plate_index": 0,
"processing_time_ms": 40.263999938964844,
"candidates": [
{
"matches_template": 0,
"plate": "AF25463",
"confidence": 94.99918365478516
}
],
"coordinates": [
{
"y": 248,
"x": 286
},
{
"y": 251,
"x": 599
},
{
"y": 318,
"x": 599
},
{
"y": 314,
"x": 286
}
],
"matches_template": 0,
"requested_topn": 10
},
"vehicle": {
"orientation": [
{
"confidence": 48.20035171508789,
"name": "270"
},
{
"confidence": 26.80327606201172,
"name": "225"
},
{
"confidence": 10.317419052124023,
"name": "315"
},
{
"confidence": 5.075159072875977,
"name": "180"
},
{
"confidence": 4.022528648376465,
"name": "0"
}
],
"color": [
{
"confidence": 89.68812561035156,
"name": "blue"
},
{
"confidence": 3.044862747192383,
"name": "silver-gray"
},
{
"confidence": 2.5986762046813965,
"name": "white"
},
{
"confidence": 2.5680367946624756,
"name": "green"
},
{
"confidence": 0.8009989857673645,
"name": "purple"
}
],
"make": [
{
"confidence": 11.725887298583984,
"name": "renault"
},
{
"confidence": 10.687095642089844,
"name": "land-rover"
},
{
"confidence": 9.455676078796387,
"name": "nissan"
},
{
"confidence": 7.793033123016357,
"name": "citroen"
},
{
"confidence": 6.8527727127075195,
"name": "suzuki"
}
],
"body_type": [
{
"confidence": 19.176368713378906,
"name": "suv-crossover"
},
{
"confidence": 18.331125259399414,
"name": "suv-standard"
},
{
"confidence": 18.325965881347656,
"name": "truck-standard"
},
{
"confidence": 11.761542320251465,
"name": "sedan-wagon"
},
{
"confidence": 10.666444778442383,
"name": "sedan-compact"
}
],
"year": [
{
"confidence": 19.741708755493164,
"name": "2005-2009"
},
{
"confidence": 17.747791290283203,
"name": "2000-2004"
},
{
"confidence": 16.381860733032227,
"name": "1995-1999"
},
{
"confidence": 13.306379318237305,
"name": "1985-1989"
},
{
"confidence": 11.591310501098633,
"name": "1980-1984"
}
],
"make_model": [
{
"confidence": 7.200094223022461,
"name": "land-rover_defender"
},
{
"confidence": 2.6545894145965576,
"name": "land-rover_discovery"
},
{
"confidence": 1.7968538999557495,
"name": "toyota_land-cruiser"
},
{
"confidence": 1.6618626117706299,
"name": "suzuki_sj-samurai"
},
{
"confidence": 1.4205354452133179,
"name": "suzuki_vitara"
}
]
},
"best_uuid": "WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410173",
"epoch_end": 1521286411970,
"best_image_width": 640,
"data_type": "alpr_group",
"best_image_height": 480,
"frame_end": 109536,
"is_parked": false,
"web_server_config": {
"agent_label": "DESKTOP-B3PMA1C",
"camera_label": "WebCamHKA"
},
"best_region": "eu-dk",
"uuids": [
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407536",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407571",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407607",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407783",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407888",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407991",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408267",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408301",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408335",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408579",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408649",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408860",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408895",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408930",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408964",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408999",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409173",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409278",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409417",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409484",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409651",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409789",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409823",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409999",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410036",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410140",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410173",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410208",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410646",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410751",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410887",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410923",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410957",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411099",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411169",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411204",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411523",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411626",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411694",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411797",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411970"
],
"plate_indexes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"travel_direction": 18.316375732421875,
"country": "eu",
"best_plate_number": "AF25463",
"best_region_confidence": 97.08333587646484,
"agent_version": "2.5.103",
"candidates": [
{
"matches_template": 0,
"plate": "AF25463",
"confidence": 94.99918365478516
},
{
"matches_template": 0,
"plate": "AF2546",
"confidence": 70.50904846191406
},
{
"matches_template": 0,
"plate": "AF254",
"confidence": 68.68018341064453
},
{
"matches_template": 0,
"plate": "AF254AF",
"confidence": 65.75811767578125
},
{
"matches_template": 0,
"plate": "A25463",
"confidence": 65.72001647949219
},
{
"matches_template": 0,
"plate": "AE25463",
"confidence": 65.68501281738281
}
]
}
EDIT:
Ohhh - I can't post an Image. I need at least 10 reputation to post it.
But I can link to it here: https://klitandersen-my.sharepoint.com/:i:/g/personal/henrik_klit-andersen_com/Ec7DQkhrcFhAgZ6wIF24MbkBgrPcbXsgSa7_msXLZDHXQw?e=cnluHm
It appears that I was on the right track thinking that this is because you have a [FromBody] string value parameter in your controller method. That seems to offset the content buffer by 1024 bytes hence why your attempt to read it as a string inside the method body yielded only part of the original request body.
You could try a few things here (at least that's the ones I can think of). Firstly, you should try to remove the parameter from. That would solve this problem right away and you would be able to just read it without any issues as you were trying now with just a single line.
var postBodyString = Request.Content.ReadAsStringAsync().Result;
Secondly, you could try to read it as a stream and reset the stream's position. If you do go with this option - make sure you use using so that everything is disposed once you are done. I know for a fact that this didn't work for me some time ago because the ReadAsStreamAsync() in Web Api is a HttpBufferlessStream and I think it doesn't support Seek operation.
var postStream = Request.Content.ReadAsStreamAsync().Result;
postStream.Seek(0, SeekOrigin.Begin);
var postBodyString = new StreamReader(postStream).ReadToEnd();
Lastly, you could try and make the parameter HttpMessageRequest which you could then read as a string.
[Route("somePost")]
[HttpPost]
public IHttpActionResult DoSomething(HttpRequestMessage request)
{
var str = request.Content.ReadAsStringAsync().Result;
return Ok();
}
Hopefully some of these options will be acceptable for you or at least get you going on the right track.
Here's some simple code that deserializes a .json file then serializes it again, making no changes to the data.
JObject json = JObject.Parse(File.ReadAllText("fileIn.json"));
JsonWriter writer = new JsonTextWriter(new StreamWriter("fileOut.json", false));
writer.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, json);
Everything seems to be deserialized just fine as the json JObject contains all the data but strangely not everything is being serialized.
If this is fileIn.json:
{
"metadata":{
"vertices":56
},
"influencesPerVertex":2,
"bones":[{
"parent":-1,
"name":"torso",
"scl":[1,1,1],
"pos":[-2.42144e-08,0.720174,-0.00499988],
"rotq":[0.707107,0,-0,0.707107]
},{
"parent":0,
"name":"head",
"scl":[1,1,1],
"pos":[0,0,-0.904725],
"rotq":[0,0,-0,1]
},{
"parent":0,
"name":"leftLeg",
"scl":[1,1,1],
"pos":[0.173333,-4.05163e-05,-0],
"rotq":[1,-4.37114e-08,-0,0]
}],
"skinIndices":[1,2,3],
"vertices":[1,2,3],
"skinWeights":[1,2,3],
"faces":[1,2,3],
"normals":[1,2,3],
"uvs":[]
}
Then fileOut.json will look like this:
{
"metadata": {
"vertices": 56
},
"influencesPerVertex": 2,
"bones": [
{
"parent": -1,
"name": "torso",
"scl": [
1,
1,
1
],
"pos": [
-2.42144E-08,
0.720174,
-0.00499988
],
"rotq": [
0.707107,
0,
0,
0.707107
]
},
{
"parent": 0,
"name": "head",
"scl": [
1,
1,
1
],
"pos": [
0,
0,
-0.904725
],
"rotq": [
0,
0,
0,
1
]
},
{
"parent": 0,
"name": "leftLeg",
"scl": [
1,
1,
1
],
"pos": [
0.173333,
-4.05163E-05,
0
],
"rotq": [
1,
-4.37114E-08,
0,
0
]
}
],
"skinIndices": [
1,
2,
3
],
"vertices": [
1,
2,
3
As you can see the output file is missing data towards the end. Why is this happening and how can I fix it? Thanks
You don't close your output file (new StreamWriter("fileOut.json", false), this is why you don't see the whole file...
A simpler way for writing indented json back to file would be
JObject json = JObject.Parse(File.ReadAllText("fileIn.json"));
File.WriteAllText("fileOut.json", json.ToString(Newtonsoft.Json.Formatting.Indented));
I have the following json string.
{
"items": {
"642163": {
"id": 642163,
"nm": "AK-21699-11-Lancer-Mohammed Al Noman",
"uid": "356307041429068",
"ph": "+971561755362",
"hwid": 216940,
"pos_x": 55.3744512,
"pos_y": 25.335552,
"pos_z": 50,
"pos_t": 1395383860,
"pos_sc": 7,
"hdop": 1.3,
"lmsg": {
"t": 1395383860,
"f": 3,
"tp": "ud",
"pos": {
"y": 25.335552,
"x": 55.3744512,
"z": 50,
"s": 0,
"c": 0,
"sc": 7
},
"i": 0,
"p": {
"param179": 0,
"param180": 1,
"param69": 1,
"param175": 1,
"param182": 13,
"hdop": 1.3,
"pwr_ext": 12.978,
"battery_charge": 0
}
},
"param180": 1,
"param69": 1,
"param182": 13,
"pwr_ext": 12.978,
"cnm": 71869,
"cnkb": 2400,
"cneh": 2160,
"client_name": "Al Kharafi",
"access_code": "sms,sms",
"serial_no": "2837965",
"vreg_number": "21699",
"instln_date": "1/14/2013",
"vehicle_type": "car_saloon",
"contact_ph": "branches"
}
},
"sid": "b96c9de5b4c609f905219c20b113f712",
"count": 12,
"p_type": "hst"
}
I want to get the id,nm,p and pos.How will i get it in asp.net c#?
Grab the JSON.NET library. It's available via NuGet under that name.
using Newtonsoft.Json.Linq;
Now you can pull data out of your JSON string yourText as in the following example.
var token = JToken.Parse(yourText);
var nm = token["items"]["642163"]["nm"].ToObject<string>();
nm will have the value AK-21699-11-Lancer-Mohammed Al Noman.