RejectedSoftware Forums

Sign up

Which function I should use to convert json-like string to JSON object?

I am always confusing which function I should to use when I am working with JSON. I have JSON-like string.

    "registered_country": {
        "iso_code": "RU",
        "names": {
            "fr": "Russie",
            "de": "Russland",
            "en": "Russia"
        },
        "geoname_id": 2017370
    }

I need to convert it ti JSON Object. How can I do it? Which of them I should to use http://vibed.org/api/vibe.data.json/serializeToJson ?

Re: Which function I should use to convert json-like string to JSON object?

Am 18.09.2017 um 15:30 schrieb Suliman:

I am always confusing which function I should to use when I am working with JSON. I have JSON-like string.

     "registered_country": {
         "iso_code": "RU",
         "names": {
             "fr": "Russie",
             "de": "Russland",
             "en": "Russia"
         },
         "geoname_id": 2017370
     }

I need to convert it ti JSON Object. How can I do it? Which of them I should to use http://vibed.org/api/vibe.data.json/serializeToJson ?

parseJsonString(text) would be the most direct way to get a Json out
of this. deserializeJson!Json(text) would be more or less equivalent,
but serialization usually only makes sense when converting to/from
statically typed objects.

Re: Which function I should use to convert json-like string to JSON object?

	string s = `"names": {
            "fr": "Russie",
            "de": "Russland",
            "en": "Russia"
        }`;
	auto x = parseJsonString(s);
	writeln(x["names"]);

Error:

std.json.JSONException@json.d(1265): (1): Error: Expected end of string after JSON value.

Re: Which function I should use to convert json-like string to JSON object?

The reason of error is missing { and } between data.

Re: Which function I should use to convert json-like string to JSON object?

On 9/18/17 11:36 AM, Suliman wrote:

The reason of error is missing { and } between data.

The error is that the string isn't valid JSON. JSON must begin with { or [.

http://www.json.org/

-Steve