1

I have a json file that looks like this:

{
  "id": 1,
  "FirstName": "jen",
  "LastName": "may",
  "UserName": "jenmay",
  "Password": "august",
  "securityQ1": "leeds",
  "securityQ2": "smith"
}{
  "id": 2,
  "FirstName": "lucy",
  "LastName": "reed",
  "UserName": "lucyreed1",
  "Password": "bucket",
  "securityQ1": "manchester",
  "securityQ2": "bow"
}

I want to read the file to check that a username and password match a users input. I have the input as uname and pword.

I first attempt to deserialize the JSON.

public List<User> DeserializedUsersList(string json)
        {
            List<User> user = JsonConvert.DeserializeObject<List<User>>(json);
            return user;
        }

it keeps throwing me a 'Newtonsoft.Json.JsonSerializationException' exception.

I'm really new to C# and I'm not sure how to find username in the file. Then check that password matches the inputted password. How should I go about this?

Thank you!

2
  • Can you add more information about the exception? Looking the file content you posted, there are two errors: 1) Array in JSON reqires the [] that are missing 2) Between the elements, a comma is needed (in your post there is no comma betweens the data of the two users) Commented Dec 13, 2020 at 21:20
  • 2
    seems like posted JSON is incomplete or incorrect Commented Dec 13, 2020 at 21:21

2 Answers 2

3

Your JSON is a bit off, meaning, you're missing commas between objects and square brackets around it. If it is an array of objects, you can use Linq to query specific user.

[{
  "id": 1,
  "FirstName": "jen",
  "LastName": "may",
  "UserName": "jenmay",
  "Password": "august",
  "securityQ1": "leeds",
  "securityQ2": "smith"
},{
  "id": 2,
  "FirstName": "lucy",
  "LastName": "reed",
  "UserName": "lucyreed1",
  "Password": "bucket",
  "securityQ1": "manchester",
  "securityQ2": "bow"
}
]

and your code would look something like this,

var user = JsonConvert.DeserializeObject<List<User>>(json);
var selectedUser = user.Where(x => x.UserName.Equals("jenmay")).FirstOrDefault();
Sign up to request clarification or add additional context in comments.

Comments

1

Json.Net support multiple content deserialization when using underlying JsonTextReader class.

var json = @"
{""id"": 1,""FirstName"": ""jen"",""LastName"": ""may"",""UserName"": ""jenmay"",""Password"": ""august"",""securityQ1"": ""leeds"",""securityQ2"": ""smith""}
{""id"": 2,""FirstName"": ""lucy"",""LastName"": ""reed"",""UserName"": ""lucyreed1"",""Password"": ""bucket"",""securityQ1"": ""manchester"",""securityQ2"": ""bow""}";


var jsonSerializer = new JsonSerializer();
using var reader = new JsonTextReader(new System.IO.StringReader(json));
reader.SupportMultipleContent = true;

var users = new List<User>();
while (reader.Read())
{
    var item = (User)jsonSerializer.Deserialize(reader, typeof(User));
    users.Add(item);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.