Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
[
	{	"id" : "hoho1"
		"pw" : "08217"
		"name" : "Potter"
	},
	{	"id" : "hoho2"
		"pw" : "1677"
		"name" : "Kane"
	},
	{	"id" : "hoho3"
		"pw" : "1541",
		"name" : "Moody"
	},
	{	"id" : "hoho4"
		"pw" : "0510"
		"name" : "christina"
	},
]

-------------------------------------------------------

C#
public class Employee
{
    public string id { get; set; }
    public string pw { get; set; }
    public string name { get; set; }
}

-------------------------------------------------------

C#
StreamReader r = new StreamReader("user.json");
string sc = r.ReadToEnd();  

Employee m = JsonConvert.DeserializeObject<Employee>(sc);

-------------------------------------------------------

C#
if (ID.Text == m.id && PW.Text == m.pw)
{
	MessageBox.Show("Login Success");

	this.Hide();
	Form2 Form = new Form2();
	Form.Show();
}
else
{
	MessageBox.Show("Login Failed");
}


What I have tried:

This is first time to programming.

I write code like that but
I got "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type Exception code.
I want to know How to work?
Posted
Updated 20-Sep-23 20:06pm
v4

That's because your JSON is not valid - it doesn't follow the rules for JSON: JSON Introduction[^]
JSON
[ { "id" : "hoho1"
"pw" : "08217"
"name" : "Potter"
},
{ "id" : "hoho2"
"pw" : "1677"
"name" : "Kane"
},
{ "id" : "hoho3"
"pw" : "1541",
"name" : "Moody"
},
{ "id" : "hoho4"
"pw" : "0510"
"name" : "christina"
},
]
You need to separate each item with commas:
JSON
[ { "id" : "hoho1",
"pw" : "08217",
"name" : "Potter"
},
{ "id" : "hoho2",
"pw" : "1677",
"name" : "Kane"
},
{ "id" : "hoho3",
"pw" : "1541",
"name" : "Moody"
},
{ "id" : "hoho4",
"pw" : "0510",
"name" : "christina"
}
]
 
Share this answer
 
Comments
Member 16097205 21-Sep-23 0:57am    
OK, I separate each item with commas then,
but I got same exception code too.

Is there any error about
StreamReader r = new StreamReader("user.json");
string sc = r.ReadToEnd();

Employee m = JsonConvert.DeserializeObject<employee>(sc); ??

I'm beginner of programming.
I need your help!!
OriginalGriff 21-Sep-23 1:52am    
Well, there are several things wrong with it, but as far as "processing JSON" goes they are not relevant: the file is stored in the wrong place so it will fail in production, the passwords are in clear text so you will get MASSIVE fines from the EU because you ignored GDPR, and your naming conventions are a poor choice.

The code itself won't work (provided you edited the JSON correctly, but I can't access your HDD to tell that! :D ) because:
1)An Employee is not the same as an employee - C# is case sensitive!
2) You aren't deserializing a single Employee instance, you are reading a collection of Employees - so you need to tell NewtonSoft that:
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(sc);
OriginalGriff 21-Sep-23 6:18am    
Have you ever heard of loops?
Member 16097205 21-Sep-23 6:25am    
for() or foreach()?
I heard that.
OriginalGriff 21-Sep-23 6:34am    
Come on man, this is basic stuff. If you can't work out "Oh, I need a loop" and "I'll use a ??? loop" for yourself, then you have very, very little chance of getting your project off the ground at all - and what you do produce will be horrendous: bloated, unreliable, hard to maintain, and very, very prone to you getting fined.

Shelve this - back it up somewhere so you can look at it later - and go back to the beginning with a course or a book and learn the basics properly before starting again. Then in six months time, drag out this project and squirm in horror at the monstrosity you were bolting together so hopefully ... :laugh:
C#
Employee m = JsonConvert.DeserializeObject<employee>(sc);

Above you're saying that there is only 1 item, not a list of items. Use:
C#
List<Employee> m = JsonConvert.DeserializeObject<List<Employee>>(sc);


UPDATE: Here is a working example using the answer above:
C#
using Newtonsoft.Json;

string rawJson = @"[ { ""id"" : ""hoho1"",
""pw"" : ""08217"",
""name"" : ""Potter""
},
{ ""id"" : ""hoho2"",
""pw"" : ""1677"",
""name"" : ""Kane""
},
{ ""id"" : ""hoho3"",
""pw"" : ""1541"",
""name"" : ""Moody""
},
{ ""id"" : ""hoho4"",
""pw"" : ""0510"",
""name"" : ""christina""
}
]";

List<Employee> results = JsonConvert.DeserializeObject<List<Employee>>(rawJson);

foreach (var item in results)
    Console.WriteLine($"id: {item.id} | pw: {item.pw} | name: {item.name}");

Console.ReadKey();

public class Employee
{
    public string id { get; set; }
    public string pw { get; set; }
    public string name { get; set; }
}

NOTE: The above code is for a .Net 7.0 console app. Same applies to .Net Framework 4.8 and earlier, just needs to be wrapped in a standard console app.

And the output:
id: hoho1 | pw: 08217 | name: Potter
id: hoho2 | pw: 1677 | name: Kane
id: hoho3 | pw: 1541 | name: Moody
id: hoho4 | pw: 0510 | name: christina
 
Share this answer
 
v2
Comments
Graeme_Grant 21-Sep-23 1:56am    
the issue is not with my answer. I'll post a full example with output.
Member 16097205 21-Sep-23 5:48am    
Then I have compile Error that There is no 'm' in current context. How can I use m?
Graeme_Grant 21-Sep-23 5:52am    
This is programming 101... it depends on where you declared it and where you are using it. Move the declaration to where you need it.

Here are a couple of links that cover this topic in more detail for beginners like yourself:
* Variable Scopes in C#[^]
* Control variable scope and logic using code blocks in C# - Training | Microsoft Learn[^]
Graeme_Grant 21-Sep-23 6:27am    
Please do not post code in comments.

What you are asking me to do is outside of the scope of Quick Answers forum. I have given you pointers to training materials to answer your variable scope issue causing your compile-time error. Now you need to roll up your sleeves and do some reading, learning and possibly more research. Good luck with it.
Dave Kreskowiak 21-Sep-23 9:30am    
It doesn't work because you don't know how any of the individual statements work. You have no clue what variable scope is, nor what the individual variables represent at any point during execution.

This all comes down to a complete lack of understanding that you are going to need books and classes to get through. NOT A FORUM ENVIRONMENT. These text boxes are way too small to type small books into to teach you everything you need to know just to get this little bit of code to work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900