I have the JObject which is coming from the client app and I am getting it on value on the controller
// POST api/APIdatavalues/5
public string Post(int id, JObject value)
{
.....
but when I am trying to use it I get an error "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'." if I dont cast to JObject and I use the just value I get a dictionary with only one key and the other values
together
This code bellow gave me the error that I a mentioned
JObject jValues = (JObject)value["JObject"];
var midictobjttest = jValues.Properties().Select(s =>
new
{
key = s.Name,
value = s.Value.ToString()
}).ToDictionary(s => s.key, s => s.value
The issue is that I cannot parse it the object is beaing porpulated with JSON that I posted in my first post here. But when I am trying to loop over it and get its values and keys on a dictionaries or my LIstof Object I cannot get that information.
this is the Json that I am recieving on the WEB API controler in a JObject
As you can see the key are numbers and values are words or number
like
Key: 20 value: us
Key: 21 value: 4,5,..
Each example on internet use an string which parse and cast to JObject end then getting the values with the ["stringket"] my problem is I am geeting that value as a JObject on the Web API controller and I tried to change the JQuery to send text and recievied
an string on the API controler but I finish having a null value.
If you just want to receive a string in your web API method you could just have a method parameter of type string and mark it as [FromBody]. To stake advantage of the JSON parsing support in Web API I would probably have to look at your project code to understand
what you are trying to do and the error you are seeing.
sinedyip
Member
308 Points
154 Posts
Parse using JObject with JSON on the WEB API
Dec 14, 2012 06:53 PM|LINK
Hi I am sending using Jquery in a post a JObject to my Web API but I am having issues using the Newtonsoft.Json library to parse it.
I am getting the data in the WEB API but i am not able to Parse using the online example.
the is at the end of this post. I need some help parsing that object. Any idea?
My controller look like this:
// POST api/returnvalues/5
public string Post(int id, JObject value)
{
var temp = value;
....
}
and my Jquery to send the information is like this.
function PostAPIRequest(address) {
var jObject = JSON.stringify(FilterValuesArray);
var responseJson = null;
$.ajax({
url: address,
type: 'POST',
dataType: 'json',
data: { JObject: jObject },
success: function (data) {
responseJson = data
ProcessDataResponse(responseJson);
//TODO: REFRESH THE DATA GRID
},
error: function (xhr, ajaxOptions, thrownError) {
//TODO redirect to the error page and send error email there.
alert(xhr.status);
alert(thrownError);
}
})
}
JSON
{
"JObject": "[{\"key\":\"20\",\"value\":\"us\"},{\"key\":\"30\",\"value\":\"mini\"},{\"key\":\"31\",\"value\":\"audi\"},{\"key\":\"21\",\"value\":\"4,5,13,14,15,\"},{\"key\":\"29\",\"value\":\"8,\"},{\"key\":\"32\",\"value\":\"7,\"}]"
}
urenjoy
Star
13455 Points
2022 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 15, 2012 05:21 AM|LINK
Have a look at following:
http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net
http://stackoverflow.com/questions/4848583/using-json-net-to-access-json-attributes-to-create-a-c-sharp-dictionary
danroth27
Member
176 Points
41 Posts
Microsoft
Re: Parse using JObject with JSON on the WEB API
Dec 15, 2012 06:46 AM|LINK
Senior Program Manager
ASP.NET
sinedyip
Member
308 Points
154 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 15, 2012 03:03 PM|LINK
those soluction did not work for me
I have the JObject which is coming from the client app and I am getting it on value on the controller
// POST api/APIdatavalues/5
public string Post(int id, JObject value)
{
.....
but when I am trying to use it I get an error "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'." if I dont cast to JObject and I use the just value I get a dictionary with only one key and the other values together
This code bellow gave me the error that I a mentioned
JObject jValues = (JObject)value["JObject"];
var midictobjttest = jValues.Properties().Select(s =>
new
{
key = s.Name,
value = s.Value.ToString()
}).ToDictionary(s => s.key, s => s.value
sinedyip
Member
308 Points
154 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 15, 2012 03:09 PM|LINK
Hi danroth.
The issue is that I cannot parse it the object is beaing porpulated with JSON that I posted in my first post here. But when I am trying to loop over it and get its values and keys on a dictionaries or my LIstof Object I cannot get that information.
this is the Json that I am recieving on the WEB API controler in a JObject
{
"JObject": "[{\"key\":\"20\",\"value\":\"us\"},{\"key\":\"30\",\"value\":\"mini\"},{\"key\":\"31\",\"value\":\"audi\"},{\"key\":\"21\",\"value\":\"4,5,13,14,15,\"},{\"key\":\"29\",\"value\":\"8,\"},{\"key\":\"32\",\"value\":\"7,\"}]"
}
As you can see the key are numbers and values are words or number
like
Key: 20 value: us
Key: 21 value: 4,5,..
Each example on internet use an string which parse and cast to JObject end then getting the values with the ["stringket"] my problem is I am geeting that value as a JObject on the Web API controller and I tried to change the JQuery to send text and recievied an string on the API controler but I finish having a null value.
Thank you
danroth27
Member
176 Points
41 Posts
Microsoft
Re: Parse using JObject with JSON on the WEB API
Dec 17, 2012 05:11 AM|LINK
Senior Program Manager
ASP.NET
sinedyip
Member
308 Points
154 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 17, 2012 10:04 AM|LINK
I could create an small example for you and send it.
sinedyip
Member
308 Points
154 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 17, 2012 01:35 PM|LINK
@danroth27
I think that my issue is the JSON format that appear with the use of JSON.stringify.
I am getting a JSON like this
"[{\"key\":\"20\",\"value\":\"ddd\"},{\"key\":\"21\",\"value\":\"4,5,13,14,15,\"}]"
and the Web API is getting
this:
{"JObject": "[{\"key\":\"20\",\"value\":\"ddd\"},{\"key\":\"21\",\"value\":\"4,5,13,14,15,\"}]"}
So I think that is the reason becuase the Parse is not working. The JSON.stringify is adding \ and the " to the JSON object.
Do you have any idea on how solved it?
Thank you in advance
sinedyip
Member
308 Points
154 Posts
Re: Parse using JObject with JSON on the WEB API
Dec 18, 2012 10:06 AM|LINK
I found the soulution and I want to share with you.
The JSON was in a wrong formatted. The soluction is format it in a right way like this post explain
http://stackoverflow.com/questions/13916673/issue-with-json-stringify-adding-a-extra-and-to-my-json-object
and the parse could be something like this and the parse works in the web api side. Assuming that is a JSON like this
private void SomeFuntion(JObject jsonrecievedinAPI )
{
JArray items = (JArray)jsonrecievedinAPI["Root"];
JObject item;
JToken jtoken;
for (int i = 0; i < items.Count; i++)
{
item = (JObject)items[i];
jtoken = item.First;
string keyFilter = string.Empty;
string valueFilter = string.Empty;
while (jtoken != null)
{
switch (((JProperty)jtoken).Name.ToString())
{
case "key":
keyFilter = ((JProperty)jtoken).Value.ToString();
break;
case "value":
valueFilter =((JProperty)jtoken).Value.ToString();
break;
}
jtoken = jtoken.Next;
}
}
}
ANILBABU
Member
67 Points
128 Posts
Re: Parse using JObject with JSON on the WEB API
May 01, 2013 09:59 AM|LINK
can you give me the sample examples plz help me
I am sendingrequest like this https://api.subtledata.com/v1/locations?api_key=SlcrNFg0
response (Output)like this
[{"revenue_centers": [{"default_center": true, "revenue_center_id": 1909, "name": "Unassigned Revenue"}], "receipt_number_instructions": null, "employee_request_through_app": false, "menu_ordering_available": true, "payment_via_credit_card_available_message": null, "postal_code": "78701", "user_rating": "0", "location_id": 918, "app_specials": false, "city": "Austin", "location_name": "notous Test Dinerware System", "tender_types": [], "process_new_credit_cards": false, "table_number_instructions": null, "state": "TX", "color_theme": null, "latitude": 30.26652, "logo_url": "http://www.subtledata.com/I/Logo/?918", "website_url": null, "cross_streets": null, "ordering_available_message": null, "phone": "8143628149", "terminals": [], "location_picture_url": "http://www.subtledata.com/I/Results/?918", "favorites_ordering_available": true, "neighborhood_name": null, "discount_types": [{"default_discount": false, "discount_type_id": 939, "name": "Employee"}, {"default_discount": false, "discount_type_id": 940, "name": "Two Dollar Tuesday"}], "longitude": -97.7429367, "price_rating": 0, "process_pre_authed_cards": false, "address_line_2": null, "address_line_1": "401 Congress Avenue"}]<div></div> </div> </div>