简体   繁体   中英

How to read JSON FILE?

I have a File with following formatted Json

{
  "Id": 0,
  "MsgId": 125,
  "ExceptionDetails": "whatever2"
}


{
  "Id": 1,
  "MsgId": 135,
  "ExceptionDetails": "whatever2"
}

This is exactly how it is in a file with no brackets.

I need to parse this text file and get the values of these Keys, for instance in this example I need to get the 0 and 1

Thanks

and this is how it is getting written to the file, so probably Im not writting to the file in correct JSON FORMAT

 string json = JsonConvert.SerializeObject(logs, Formatting.Indented);
 using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\development\commonArea\WriteLines.txt", true))
                {

                    file.WriteLine(json);

                }

Is that your raw file? If so, it's not valid json.

What you can do in your case is to use string splitting or regex-fu to split your file into separate json objects and then parse them using either the built-in JavaScriptSerializer or Newtonsoft.Json.

var x = [{
  "Id": 0,
  "MsgId": 125,
  "ExceptionDetails": "whatever2"
},


{
  "Id": 1,
  "MsgId": 135,
  "ExceptionDetails": "whatever2"
}]

x[0].Id // 0
x[1].Id // 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM