79482819

Date: 2025-03-04 06:39:38
Score: 0.5
Natty:
Report link

You need to use create the json with

// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();

b/c the code checks if it's a object in the function

    ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
    {
        // value only works for objects
        if (JSON_HEDLEY_LIKELY(is_object()))
        {
            // if key is found, return value and given default value otherwise
            const auto it = find(key);
            if (it != end())
            {
                return it->template get<ReturnType>();
            }

            return std::forward<ValueType>(default_value);
        }

        JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
    }

https://github.com/nlohmann/json/blob/develop/include/nlohmann/json.hpp#L2281

The json can be a string, number, or array etc.

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: K.Robert