I have this JSON:
{"id":1,"name":"Alabama"}
{"id":2,"name":"Alaska"}
{"id":3,"name":"Arizona"}
{"id":4,"name":"Arkansas"}
and this code:
string[] testDataFiles = new string[] { StatesOneFileName, StatesTwoFileName, ContinentsFileName };
foreach (var testFile in testDataFiles)
{
using (StreamReader r = new StreamReader(testFile))
{
string json = r.ReadToEnd();
dynamic jsonObjectArray = JsonConvert.DeserializeObject(json);
foreach (var item in jsonObjectArray)
{
expectedPartitions.Add(item.name);
}
}
}
When I run this, however, I get this error:
Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.
I've read a few StackOverflow suggestions to encase the test data in [], but unfortunately I cannot edit the json file itself. Is there a way to wrap something in my code such that it reads the correct way? Thanks.
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();
If this is your json file, it's not correctly formatted... You have multiple json objects in it, but it's neither enclosed in [] to show it's an array of json objects, nor are they delimited by comma.
The best thing you can do here, is to read line at a time, and convert it to json object, since it seems that each line represents one json object.
string json = "start";
using (StreamReader r = new StreamReader(testFile)) {
while (!String.IsNullOrWhiteSpace(json)){
json = r.ReadLine();
dynamic jsonObject = JsonConvert.DeserializeObject(json);
//DO YOUR MAGIC HERE
}
}
Here's one way you could easily fix the syntax errors in the json from that file, without modifying your existing code too much:
foreach (var testFile in testDataFiles)
{
// read the contents of the file with invalid json format, and build a new string named cleanJson with the fixed file contents
string[] fileLines = File.ReadAllLines(testFile);
string cleanJson = "["; // add an opening array bracket to make it valid json syntax
for (int i = 0; i < fileLines.Length; i++)
{
cleanJson += fileLines[i];
if (i < fileLines.Length - 1)
cleanJson += ","; // add a comma to the end of each line (except the last) to make it valid json syntax
}
cleanJson += "]"; // add a closing array bracket to make it valid json syntax
dynamic jsonObjectArray = JsonConvert.DeserializeObject(cleanJson);
foreach (var item in jsonObjectArray)
{
expectedPartitions.Add(item.name);
}
}
Related
I'm getting a JSON response back from another website and then building the response from a StreamReader:-
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
response = streamReader.ReadToEnd();
}
The result I'm getting is:-
string response = "{\"d\":\"[{\\\"Animal\\\":\\\"Cat\\\",\\\"Noise\\\":\\\"Meow\\\"},{\\\"Animal\\\":\\\"Dog\\\",\\\"Noise\\\":\\\"Woof\\\"}]\"}";
I've then used the JsonConvert.DeserializeObject(response) to deserialise and then I'm then trying to loop through the results to read the values.. but it's not working whatever I try
dynamic jObj = JsonConvert.DeserializeObject(response);
var arr = jObj; //Tried var arr = jObj.d;
#foreach (var item in arr)
{
….
}
Error: Target type System.Collections.IEnumerable is not a value type or a non-abstract class.
Parameter name: targetType
Your JSON response contains a d property, which value is an array wrapped into string itself.
So, you should parse a d content separately into array, Json.Linq solution is below
string response = "{\"d\":\"[{\\\"Animal\\\":\\\"Cat\\\",\\\"Noise\\\":\\\"Meow\\\"},{\\\"Animal\\\":\\\"Dog\\\",\\\"Noise\\\":\\\"Woof\\\"}]\"}";
var json = JObject.Parse(response);
var array = json["d"];
foreach (var item in JArray.Parse(array.ToString()))
{
Console.WriteLine(item["Animal"]);
}
Solution with deserialization to dynamic object
dynamic json = JsonConvert.DeserializeObject(response);
var array = json?.d;
foreach (var item in JsonConvert.DeserializeObject(array?.ToString()))
{
Console.WriteLine(item?.Animal);
}
It allows you to parse the response without any changes to source JSON
If you printed response, you would see that jObj.d is not an array, but a string. In fact, it looks like the string representation of an array of objects.
It seems that the "d"'s value contains another JSON data. If it is correct and you don't want to touch the JSON format, here is the solution:
string response = "{\"d\":\"[{\\\"Animal\\\":\\\"Cat\\\",\\\"Noise\\\":\\\"Meow\\\"},{\\\"Animal\\\":\\\"Dog\\\",\\\"Noise\\\":\\\"Woof\\\"}]\"}";
dynamic jObj = JsonConvert.DeserializeObject(response);
var arr = JsonConvert.DeserializeObject<D[]>((jObj.d).Value);
foreach (var item in arr)
{
}
........
public class D
{
public string Animal { get; set; }
public string Noise { get; set; }
}
I'm creating software where the users can create and load profiles to fill textboxes. The names and other information contained in the profile are stored in a JSON file. A profile name can contain any text that is entered by the user.
So for this, I'm trying to get each objects names of the JSON file (= each profile name) to display them in a treeview, but all I get is their contents.
I have a JSON file containing two objects:
[
{
"profile1": {
//Some informations 1
},
"profile2": {
//Some informations 2
}
}
]
For now, I have code that allows me to get the value of a given tag, but I can't find a way to get the name of each objects:
using (StreamReader r = File.OpenText(path))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
debug_tb.Text += item.profile1; //Gives me each values of the "profile1 object"
}
}
So what I'm trying to get is to display "profile1" and "profile2" and "profile3" if it exists.
Your problem is that your JSON is an array with one object. So you can simplefy the JSON first:
{
"profile1": {
//Some informations 1
},
"profile2": {
//Some informations 2
}
}
Then you can easyly iterate over every item in the JSON and get the Name of it
dynamic array = JsonConvert.DeserializeObject("{ \"profile1\": { }, \"profile2\": { } }");
foreach (var item in array)
{
debug_tb.Text += item.Name; //Gives the name of the object
}
Console.WriteLine(text);
Console.ReadLine();
I am trying to get data from web form and convert properties into a list of json objects through json.net, first time a list of object is stored in a file. But next time, instead of appending data into list it creates new list. So how can I add it in same list. Here is my code
public List<StudentProps> GetProps(StudentProps p)
{
List<StudentProps> s = new List<StudentProps>();
s.Add(p);
return s;
}
public void GetStudent(StudentProps p)
{
var json = JsonConvert.SerializeObject(GetProps(p), Formatting.Indented);
String FilePath = #"C:\Users\Haseeb\Desktop\Test.json";
File.AppendAllText(FilePath, json + Environment.NewLine);
var ReadFile = File.ReadAllText(FilePath);
// List<StudentProps> props = JsonConvert.DeserializeObject<List<StudentProps>>(ReadFile).ToList();
}
I'm trying to automate the addition of new objects to an existing JSON file. I looked all around the web but only found adding data and stuff but not a whole object. This is how the file that I want to edit looks:
[
{"id":"123","name":"carl"}
]
and I want to go to
[
{"id":"123","name":"carl"},
{"id":"1234","name":"carl2"}
]
Thank you for all your answers but I don't think everyone completely understands what i mean I have tried some of the answers but then I get this:
[
"{\"id\":\"123\",\"name\":\"carl\"}"
]"{\"id\":\"1234\",\"name\":\"carl2\"}"
and I want everything in between the [].
If you use json.NET you can simply deserialize and serialize the json.
var list = JsonConvert.DeserializeObject<List<Person>>(myJsonString);
list.Add(new Person(1234,"carl2");
var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented);
Using Json.Net
//load from file
var initialJson = "[{\"id\":\"123\",\"name\":\"carl\"}]";
var array = JArray.Parse(initialJson);
var itemToAdd = new JObject();
itemToAdd["id"] = 1234;
itemToAdd["name"] = "carl2";
array.Add(itemToAdd);
var jsonToOutput = JsonConvert.SerializeObject(array, Formatting.Indented);
//save to file here
Using this method doesn't require strongly typed objects
You could replace this bit:
//load from file
var initialJson = "[{\"id\":\"123\",\"name\":\"carl\"}]";
With
var initialJson = File.ReadAllText(#"c:\myjson.json")
To load the json from a text file
A better performing solution than serializing/deserializing what may be a large file would be to open a FileStream, seek 1 character before the end, then serialize and write your new object into the array, then write a closing bracket. See this question C# How to delete last 2 characters from text file and write into the same line at the end my text?, I'll copy the code here - you already know how to serialize your object and encode it into bytes.
using(var fs = new FileStream("file.json")) {
fs.Seek(-1,SeekOrigin.End);
fs.Write(mySerializedJSONObjAsBytes,0,mySerializedJSONObjAsBytes.Length); // include a leading comma character if required
fs.Write(squareBracketByte, 0, 1);
fs.SetLength(fs.Position); //Only needed if new content may be smaller than old
}
Sorry haven't tested any of that, it's off the top of my head. Pro-tip: wrap FileStream in a StreamWriter so can write strings directly.
You could create a method:
public string AddObjectsToJson<T>(string json, List<T> objects)
{
List<T> list = JsonConvert.DeserializeObject<List<T>>(json);
list.AddRange(objects);
return JsonConvert.SerializeObject(list);
}
Then use it like this:
string baseJson = "[{\"id\":\"123\",\"name\":\"carl\"}]";
List<Person> personsToAdd = new List<Person>() { new Person(1234,"carl2") };
string updatedJson = AddObjectsToJson(baseJson, personsToAdd);
this would be a sample for you:
var list = JsonConvert.DeserializeObject<List<Example>>(json);
Example example = new Example();
example.name = "Product2";
example.id="1";
list.Add(example);
string output = JsonConvert.SerializeObject(list);
public class Example
{
public string id {get;set;}
public string name { get; set; }
}
I have been looking for a solution to this exact question for a week now. I finally figured out how to append it to the existing json array and not add it as a solo object after the array. Make sure you are using WriteAllText and not AppendAllText. Here is what I did:
JArray array = JsonConvert.DeserializeObject<JArray (jsonFile);
JObject obj = new JObject();
obj.Add("ID", "123");
obj.Add("Name", "Brian");
array.Add(obj);
var jsonToOutput = JsonConvert.SerializeObject(array, Formatting.Indented);
File.WriteAllText("fileName.json", jsonToOutput);
I have a class with four fields (DateTime, Enum, string, string). I want to serialize it to and from an XML element or a series of XML elements in a compact manner. For example, I might serialize it to something like this:
<i t='234233' a='3'><u>Username1</u><s1>This is a string</s1></i>
<i t='234233' a='4'><u>Username2</u><s1>This is a string</s1></i>
<i t='223411' a='1'><u>Username3</u><s1>This is a string</s1></i>
Where 'i' is each class instance, 't' is the DateTime ticks, 'a' is the enum value, and the elements are strings.
I'd prefer to not have a root element, but if I do have it, I'd like it to be as small as possible.
I've tried using XmlSerializer with the XmlWriterSettings class but I can't get rid of the namespaces and root element.
What's the best way of doing this? I'm not saving to a file, I'm reading and writing to strings in memory.
System.Xml.Linq
XElement xElem = new XElement("r");
for (int i = 0; i < 3; i++)
{
xElem.Add(
new XElement("i",
new XAttribute("t", "234233"),
new XAttribute("a", "3"),
new XElement("u", "UserName"),
new XElement("s1", "This is a string")
)
);
}
var str = xElem.ToString();
and to read
XElement xElem2 = XElement.Load(new StringReader(str));
foreach(var item in xElem2.Descendants("i"))
{
Console.WriteLine(item.Attribute("t").Value + " " + item.Element("u").Value);
}
PS:
You don't need to convert xElem to string in order to use that xml in memory
If your data is that simple, you can use XmlWriter directly:
class Data {
public DateTime Date { get; set; }
public int Code { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
static void Main() {
var sb = new StringBuilder();
var xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = false;
var elements = new[] {
new Data { Date = DateTime.Now, First = "Hello", Last = "World", Code = 2}
, new Data { Date = DateTime.UtcNow, First = "Quick", Last = "Brown", Code = 4}
};
using (var xw = XmlWriter.Create(sb, xws)) {
xw.WriteStartElement("root");
foreach (var d in elements) {
xw.WriteStartElement("i");
xw.WriteAttributeString("t", ""+d.Date);
xw.WriteAttributeString("a", "" + d.Code);
xw.WriteElementString("u", d.First);
xw.WriteElementString("s1", d.Last);
xw.WriteEndElement();
}
xw.WriteEndElement();
}
Console.WriteLine(sb.ToString());
}
Running this program produces the following output (I added line breaks for clarity; they are not in the output):
<root>
<i t="2/9/2012 3:16:56 PM" a="2"><u>Hello</u><s1>World</s1></i>
<i t="2/9/2012 8:16:56 PM" a="4"><u>Quick</u><s1>Brown</s1></i>
</root>
You need that root element if you would like to read the information back. The most expedient way would be using LINQ2XML:
var xdoc = XDocument.Load(new StringReader(xml));
var back = xdoc.Element("root").Elements("i").Select(
e => new Data {
Date = DateTime.Parse(e.Attribute("t").Value)
, Code = int.Parse(e.Attribute("a").Value)
, First = e.Element("u").Value
, Last = e.Element("s1").Value
}
).ToList();
I'd just use a StringBuilder, personally.
If size is your #1 concern, consider json or yaml instead of XML.
You'll need to implement your own serializer, I believe, which will be a pain. That, or you could manually strip out what you don't need after serializing, which could work.
If size is your concern, you should be serializing to binary using BinaryFormatter. You can always base-64 encode it if you need to store it as a string. (BinaryFormatter works almost just like XmlSerializer, except the output is raw binary, rather than nicely formatted XML.)