This question already has answers here:
C# using streams
(7 answers)
Easiest way to read from and write to files
(14 answers)
Closed 1 year ago.
In Xamarin.Forms project I have JSON file saved as Embedded Resource. I get it as FileStream by calling:
var assembly = typeof(App).GetTypeInfo().Assembly;
FileStream fileStream = assembly.GetFiles()[0];
How do I parse this stream to json string?
I know it is most likely super dumb question but I am confused, all I achieved was some random characters.
First, get the raw string from the JSON file by using
string jsonText = File.ReadAllText("/path/to/file.txt")
From there, you can convert the JSON string into an object (if needed):
MyClass jsonObj = JsonSerializer.Deserialize<MyClass>(jsonText);
Don't forget to import System.Text.Json.Serialization, System.Text.Json, and System.IO when working with JSON files.
You're having errors because you're trying to get a literal FileStream, which is not the intended use for it. You can read more about serializing JSONs here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0
Related
This question already has answers here:
How do you read a simple value out of some json using System.Text.Json?
(8 answers)
Closed 11 months ago.
From an API call I am getting some JSON data, I dont want to declare a class, while in NewtonSoft we can get those data as given below without creating a class,
JObject o1 = JObject.Parse(File.ReadAllText(#"example.json"));
Console.WriteLine(o1["customername"]);
Is there any similar way to get json data without creating a class in System.Text.Json
Edit:
I was able to proceed by the solutions provided here
How do you read a simple value out of some json using System.Text.Json?
You could try using JSON document model and parse subsections you are interested in.
See this link https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter?pivots=dotnet-6-0#deserialize-subsections-of-a-json-payload
This question already has answers here:
How to read an entire file to a string using C#?
(17 answers)
Closed 5 years ago.
So I have this .json file: temp_file.json
Now all I need to do is get whatever is in this .json file, and put it in a string using C# in Visual Studio 2017.
That's it.
I don't want it to be turned into a object of a certain class or whatever. Just get whatsever in the file, right into a string.
A lot of other questions/answers I have stumbled upon are about desirializing and serializing etc. I don't need that. Just turn the .json file in to a string. No need to write it to a console or whatsoever.
Somewhy I just cant lay my finger on it. It sounds simple to do...
You dont get any simpler than
var contents = File.ReadAllText(#"drive:\path\to\yourfile.json");
This question already has answers here:
How to read embedded resource text file
(23 answers)
Closed 9 years ago.
I have a text file data.txt that's embedded in my solution (as described in this SO question).
How do I read the contents of this file as a string? I'm imagining something like this:
string data = Resources["data.txt"];
but that's not the way to do it.
If you add the file as a resource you should be able to access it like this:
Properties.Resources.data
Or alternatively if you set the Copy to Output Directory property to Copy always/Copy if newer, you can do something like:
using (FileStream fs = System.IO.File.Open("Resources/data.txt", FileMode.Open))
{
// do amazing stuff here ...
}
This question already has answers here:
How to set formatting with JavaScriptSerializer when JSON serializing?
(4 answers)
Closed 7 years ago.
I am using JavaScriptSerializer for a list - and it works:
string codeString = (new JavaScriptSerializer()).Serialize(ramGraphList);
Although the output is formatted as follows:
[["Time","Value"],["08/07/2012 12:43:28","5270"],["08/07/2012 12:44:32","5277"]];
The problem is that the values "5270" and "5277" are not strings, they should be treated as int's hence they have to be unquoted.
Is there an effective way of achieve this?
Expected output:
[["Time","Value"],["08/07/2012 12:43:28",5270],["08/07/2012 12:44:32",5277]];
I believe that it's not possible to do this with the JavaScriptSerializer class without wraping it or modifying the class. However, have you considered using DataContractJsonSerializer or JSON.Net?
This question already has answers here:
Deserialize JSON into C# dynamic object?
(31 answers)
Closed 8 years ago.
I have some working code:
String objstr = "{\"m_children\":[{\"m_children\":null,\"m_name\":\"child0\"},{\"m_children\":null,\"m_name\":\"child1\"}],\"m_name\":\"Root\"}";
byte[] byteArr = Encoding.ASCII.GetBytes(objstr);
MemoryStream ms = new MemoryStream(byteArr);
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Node));
Node obj = (Node)ser.ReadObject(ms);
What bugs me is that I have to know the type of the object contained in the string before I decode it. I wanted to send an object encoded in JSON over a TCP pipe, and not have to send extra information about what type the object is.
With .NET 4.0 you can use dynamic objects. Why not try out this solution from another question: Deserialize JSON into C# dynamic object?
Additional info:
http://www.codeproject.com/KB/IP/fastJSON.aspx
When this library encodes JSON, it gives enough info for completely automatic reparsing.
This doesn't suit my purpose that well though, since it includes a bunch of C#-specific info, and we're working cross-language here.