i cant create a post using the version 2.0.1 on asp.net CORE
This is the Code:
var client = new WordPressClient( "http://sitioweb.com/wp-json/" );
client.Auth.UseBasicAuth( "username", "app_password" );
var post = new Post() {
Title = new Title( "ITCmx" ),
Content = new Content( "Content PostCreate" )
};
var createdPost = await client.Posts.CreateAsync( post );
And get this error
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'WordPressPCL.Models.Post' because the type requires a JSON object
(e.g. {"name":"value"}) to deserialize correctly. To fix this error
either change the JSON to a JSON object (e.g. {"name":"value"}) or
change the deserialized type to an array or a type that implements a
collection interface (e.g. ICollection, IList) like List that can be
deserialized from a JSON array. JsonArrayAttribute can also be added
to the type to force it to deserialize from a JSON array. Path '',
line 1, position 1.
I try on Postman and works fine
Could you please help me
Thank you
You need to convert your data to json before sending
using Newtonsoft.Json;
var json = JsonConvert.SerializeObject(post);
var createdPost = await client.Posts.CreateAsync( json);
I would like to read a JSON string via the Newtonsoft Json library. It works fine for any basic datatype, but it does not work for a List<double> or any List for that matter.
The test application looks like the following:
static void main()
{
string jsonString = #"
{
'name': 'set1',
'Xvv': {
'parameter': 'hByT',
'values': '[1,2,3]'
}
}";
JObject Json = JObject.Parse(jsonString);
var name = Json["name"].ToString();
var data = Json["Xvv"]["values"].Value<List<double> >(); // Raises error
}
The last line throws the following exception:
System.InvalidCastException: Invalid cast from 'System.String' to 'System.Collections.Generic.List
Is there a way to access the data directly as a List<double>?
In the example JSON you've provided, values is a string. A proper JSON array would be
'values': [1,2,3]
Anyway, after changing the string to the array, .Value<List<double>>() would throw an exception, that a JArray cannot be cast to a JToken - unfortunately I do not really know, why it does not work.
However, JToken.ToObject<T> does the trick, it
Creates an instance of the specified .NET type from the JToken
(see the documentation for ToObject)
With the line
var data = Json["Xvv"]["values"].ToObject<List<double>>();
you can cast the array correctly.
If an IEnumerable would be fine for you, too, you could also use
var data = Json["Xvv"]["values"].Values<double>();
I am trying to get all the images from an imgur album. It works fine with all of the albums, except when i try to get images from
this album
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[Imgur.API.Models.Impl.Image]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Here is my code, which throws out the exception.
public async Task<List<IImage>> dosomething()
{
IEnumerable<IImage> image = await Task.Run(AccessTheWebAsync);
return image.ToList(); ;
}
async Task<IEnumerable<IImage>> AccessTheWebAsync()
{
var client = new ImgurClient(token);
var endpoint = new AlbumEndpoint(client);
IEnumerable<IImage> images = await endpoint.GetAlbumImagesAsync(albumlink);
return images;
}
The value for albumlink that you're passing is incorrect, it should not have the #0 on the end.
The value for albumlink should just be Dc2k6.
I'm not sure why the #0 is there, I assume it can select a specific image or something but I couldn't get it to work. To prove it's irrelevant, add any other value after the #:
https://imgur.com/a/Dc2k6#SAUSAGES
I need to deserialize json file with unknown keys and unknown ammount of keys
{"key1":"val01", "key2":"val02", "key3":"val03", ..., "keyn":"val0n"}
{"key1":"val11", "key2":"val12", "key3":"val13", ..., "keyn":"val1n"}
{"key1":"val21", "key2":"val22", "key3":"val23", ..., "keyn":"val2n"}
I don't know the keys and even don't know n.
I've tried to parse it as list of dictionaries:
res = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsontext);
but I've received an exception:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[System.Collections.Generic.Dictionary2[System.String,System.String]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '/APL/HVAC/TREG/EACT', line 1, position 23..
How to get rid of this exception?
Your json is not valid. You can validate your json strings with tools like JSON Formatter
Lists should have [ in the beginning and ] in the end. You also need to put commas between elements.
A valid version of your string would be like this:
[
{
"key1": "val01",
"key2": "val02",
"key3": "val03",
"keyn": "val0n"
},
{
"key1": "val11",
"key2": "val12",
"key3": "val13",
"keyn": "val1n"
},
{
"key1": "val21",
"key2": "val22",
"key3": "val23",
"keyn": "val2n"
}
]
JSON Editor Online lets you both check and edit.
Try this:
using Newtonsoft.Json.Linq;
string json = "[{\"key1\":\"val01\", \"key2\":\"val02\", \"key3\":\"val03\", \"keyn\":\"val0n\"}, {\"key1\":\"val11\", \"key2\":\"val12\", \"key3\":\"val13\", \"keyn\":\"val1n\"}, {\"key1\":\"val21\", \"key2\":\"val22\", \"key3\":\"val23\", \"keyn\":\"val2n\"}]";
var objects = JArray.Parse(json); // parse as array
foreach(JObject obj in objects)
{
foreach(KeyValuePair<String, JToken> pair in obj)
{
Console.WriteLine(pair.Key + " -> " + pair.Value);
}
}
your json file is maybe malformed, it should start with [ and end with ] to be readable with a Dictionary<string, string>
Exemple:
[{"key1":"val01"}, {"key2":"val02"}, {"key3":"val03"}, ..., {"keyn":"val0n"}]
I've dealt with this problem using this:
var result = new List<Dictionary<string, string>>();
var jsons = jsontext.Split('\n');
foreach (var x in jsons)
{
var res = JsonConvert.DeserializeObject<Dictionary<string, string>>(x);
result.Add(res);
}
I'm trying to get tweets at the homepage of twitter. But, JsonSerializationSettingsException is thrown:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TweetSharp.TwitterStatus' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
listBox1.Items.Clear();
var option = new ListTweetsOnHomeTimelineOptions() { Count = 25, IncludeEntities = false };
var tweets = service.ListTweetsOnHomeTimeline(option);
foreach (var i in tweets)
{
string tweet = String.Format("{0} -> {1}", i.User.ScreenName, i.Text);
listBox1.Items.Add(tweet);
}