-1

I have some JSON formatted like this:

[
    {
        "name": "jsonvalue"
    }
]

And I want to print it like println!("{:?}", json["name"]);. How do I access a JSON array from a serde_json::Value?

I would like to not have to use more variables to break it down is the point.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
salmon
  • 31
  • 4

1 Answers1

-1

You can access an element from a JSON array by the index operator [0]:

let value = json!([{"name":"jsonvalue"}]);
println!("{}", value[0]["name"]);
"jsonvalue"

Note that the index operator will panic if the element doesn't exist, but you can use .get() to handle that case since it returns an Option.

kmdreko
  • 42,554
  • 6
  • 57
  • 106