-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a77b432
commit f7bb2d6
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,6 +319,37 @@ func main() { | |
} | ||
``` | ||
### Read JSON by File | ||
If you're working with the JSON dataset stored in a file (with the extension `.json` or `.txt`), you can use the `ParseBufio(in io.Reader)` function to read all the content of the JSON into the `Context` struct. | ||
eg. | ||
```go | ||
package main | ||
import ( | ||
"fmt" | ||
"os" | ||
"github.com/sivaosorg/fj" | ||
) | ||
func main() { | ||
file, err := os.Open("./logs/data.json") | ||
if err != nil { | ||
return | ||
} | ||
defer file.Close() | ||
ctx := fj.ParseBufio(file) | ||
value := ctx.Get(`stock.0.symbol`) | ||
fmt.Println(value.String()) // MMM | ||
value = ctx.Get(`bank.0.@minify`) | ||
fmt.Println(value.String()) // {"isActive":false,"balance":"$1,404.23","age":26,"eyeColor":"blue","name":"Stark Jenkins","gender":"male","company":"HINWAY","email":"[email protected]","phone":"+1 (943) 542-3591","address":"766 Cooke Court, Dunbar, Connecticut, 9512"} | ||
} | ||
``` | ||
### Validation | ||
The `Get*` and `Parse*` functions assume that the JSON is properly structured. Invalid JSON won't cause a panic, but it could lead to unexpected outcomes. | ||
|