Random Name generator.
I wanted to learn by doing practice and reading books (hate video tutorial format).
This is what I had in mind and I was able to get most of it done.
1) Capture set of names in string array
2) Get the length of the array and store the value in variable
3) Use the length stored to create random number store that in variable
4) used the random numbered to print random names from array
This is all working using code below, my last requirement was, Load the name from JSON file. For the life of it i can't get it to load as Array. I was able to load it but it's printing value by specific position of character not array position.
Code:
https://paste.mod.gg/wracquwvdibv/0
BlazeBin - wracquwvdibv
A tool for sharing your source code with the world!
17 Replies
jsonfile
is a string, which works as a character array, so jsonfile[13]
is the 14th character in that file
You need to use JsonSerializer.Deserialize<string[]>
to parse the raw json string into a C# arrayI was able to get an error to come up this time, looks like some syntex issue?
This is the new code
string sample2 = JsonSerializer.Deserialize<string>("C:\\Users\\Home\\Documents\\CSharp\\HelloWorld\\RandomName\\CombineFirstNameDb.json");
Console.WriteLine(sample2[10]);
That's the path to the file, not its content
error i am getting is,
`System.Text.Json.JsonException
HResult=0x80131500
Message='C' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
Source=System.Text.Json
Right, because that's not JSON
That's the file path
You need to read the JSON from the file with
File.ReadAllText()
, and then deserialize that JSON data to an arrayahh got it, I was trying to skip over a step. Let me do the same using what I had before.
baby steps...
I changed it to use this
string jsonfile = File.ReadAllText(@"C:\Users\Home\Documents\CSharp\HelloWorld\RandomName\CombineFirstNameDb.json");
string sample = JsonSerializer.Deserialize<string>(jsonfile);
Console.WriteLine(sample[0]);
Getting error below. I did confirm no error on json file that's loaded in VS and online as well.
System.Text.Json.JsonException: 'The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
InvalidOperationException: Cannot get the value of a token type 'StartArray' as a string.
You're not deserializing it to an array
again,
sample[0]
is not doing what you think it does
strings are character arrays; you want a string arrayFixed it, I had to re-read your above statement.
This solved it.
string jsonfile = File.ReadAllText(@"C:\Users\Home\Documents\CSharp\HelloWorld\RandomName\CombineFirstNameDb.json");
var sample = JsonSerializer.Deserialize<string[]>(jsonfile);
Console.WriteLine(sample[0]);
yep, there you go
Thanks for your patience and not providing the actual solution. I never thought I would actually use var at all.
you can use var all the time, and should
var
just infers the type of the expression from the right hand side of the equals sign
in this case there's no difference between writing var sample
and string[] sample
you just can't write string sample
, because the result of Deserialize<string[]>
isn't a string
it's a string[]
Ok, that make sense as well.
oh yeah, as I used
string[] firstName = [ "Foo", "Bar", "Foo2", "Bar2" ];
at the top. using external file, I had to use the same logic just data input as json instead of manual.yep
I assumed json input would magically conver it as string[]
the deserialization does, you just have to tell it that you're looking for a
string[]
Got you,
well, if you ever need Random baby name maker, you know where to look :), Thanks again!
lmao ofc!