Additional text encountered after finished reading JSON content:
You need to surround that with square brackets, which denotes that it's an array:
[{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]
Additional text encountered after JSON reading
Assuming a file that contains a json object per line, as follows:
{"id":1,"name":"Alabama"}
{"id":2,"name":"Alaska"}
{"id":3,"name":"Arizona"}
{"id":4,"name":"Arkansas"}
You can create the following model for your data:
public class State
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name{ get; set; }
}
And then create a simple method that reads the file line by line, as follows:
public static IEnumerable<State> GetStates(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("Path to json file cannot be null or whitespace.", nameof(path));
}
if (!File.Exists(path))
{
throw new FileNotFoundException("Could not find json file to parse!", path);
}
foreach (string line in File.ReadLines(path).Where(x => !string.IsNullOrWhiteSpace(x)))
{
State state = JsonConvert.DeserializeObject<State>(line);
yield return state;
}
}
Using the code:
string path = ".....";
// get the list of State objects...
List<State> states = GetStates(path).ToList();
// or just the list of state names
List<string> stateNames = GetStates(path).Select(x => x.Name).ToList();
Deserializing json throwing 'Additional text encountered' error
I would deserializeObject Json data to be List<WebsiteItemViewModel>
collection object by a method.
private List<WebsiteItemViewModel> readData()
{
string data = File.ReadAllText("Testfile");
return JsonConvert.DeserializeObject<List<WebsiteItemViewModel>>(data) ?? new List<WebsiteItemViewModel>();
}
then you can append your WebsiteItemViewModel
type WebItem
new object in the collection the use WriteAllText
to overwrite the file to make sure the JSON data is the newest.
var list = readData();
WebsiteItemViewModel WebItem = new WebsiteItemViewModel
{
Title = "Twitter Item",
Description = "Here is a long description that might be crossing the bounds but thats fine.",
Image = "../resources/twitter.png"
};
list.Add(WebItem);
string resultJson2 = JsonConvert.SerializeObject(list);
File.WriteAllText("Testfile", resultJson2);
NOTE
use WriteAllText
instead of AppendAllText
because AppendAllText
will append the JSON data on the end.
WriteAllText
will overwrite the file to keep your JSON be a valid array string.
Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content:
After alot of work, I came up with this:
string final = string.Empty;
string name = encoder.GetString(buffer);
char []arr = name.ToArray();
boolean bln = true;
foreach (char item in arr)
{
if (bln)
{
if (item == '}')
{
final += item.ToString();
break;
}
else
{
final += item.ToString();
}
}
}
Console.WriteLine(final);
which will truncate the rest of the characters.
Additional text encountered after finished reading JSON content and Unexpected token , in my json
{ ... }
is valid JSON. After that, ,
is an unexpected token. After that, { ... }
is trailing garbage.
Related Topics
Casting List<T> - Covariance/Contravariance Problem
Setting Unique Constraint with Fluent API
How to Check If Any Flags of a Flag Combination Are Set
I Need a Fast Runtime Expression Parser
Iterating Through All Nodes in Xml File
Unable to Set Row Visible False of a Datagridview
Delegate Caching Behavior Changes in Roslyn
How to Read Regex Captures in C#
Can You Configure Log4Net in Code Instead of Using a Config File
How to Use C# to Sanitize Input on an HTML Page
Calculated Column in Ef Code First