Share via


unable to load string into JSON

Question

Thursday, May 26, 2016 7:26 PM

Hi,

I have a string like below, and I need to access the values in "tag" field, how can I do that? I tried loading it into JSON but its giving me error. Can anyone help?

{g('ads.displayMasterAd', {    adUnit:      "/5641/ted2/talk",    containerId: "master-gpt",    targeting:   {"id":2503,"talk":"moran_cerf_this_scientist_can_hack_your_dreams","tag":"brain,communication,future,hack,identity,intelligence,life,mind,science,technology","year":"2016","event":"TED2016"}  });}

Thanks.

sujit

All replies (4)

Thursday, May 26, 2016 8:23 PM âś…Answered

Hi,

you get the error while try to load this as JSON string, because this isn't valid JSON.

{
    g('ads.displayMasterAd',
    {
        adUnit: "/5641/ted2/talk",
        containerId: "master-gpt",
        targeting: {
            "id": 2503,
            "talk": "moran_cerf_this_scientist_can_hack_your_dreams",
            "tag": "brain,communication,future,hack,identity,intelligence,life,mind,science,technology",
            "year": "2016",
            "event": "TED2016"
        }
    });
}

You can use an online JSON validator to see that it isn't valid JSON (e.g. http://jsonlint.com/)

Therefor you have the following options:

- The string is very similar to JSON, so you could write a transform methode to make valid JSON out of the string

- You can use different types of string parsing to get the "tag" field like regex

As example the way you could parse it using regex:

static void Main(string[] args)
{
    string data = "{g('ads.displayMasterAd', {    adUnit:      \"/5641/ted2/talk\",    containerId: \"master-gpt\",    targeting:   {\"id\":2503,\"talk\":\"moran_cerf_this_scientist_can_hack_your_dreams\",\"tag\":\"brain,communication,future,hack,identity,intelligence,life,mind,science,technology\",\"year\":\"2016\",\"event\":\"TED2016\"}  });}";

    var reg = new Regex("\"tag\"\\s*:\\s*\"([\\w\\d,]*)\"");
    var matches = reg.Matches(data);

    foreach(Match match in matches)
    {
        if(match.Groups.Count <= 1)
        {
            continue;
        }
        string res = match.Groups[1].Value;
        Console.WriteLine(res);
    }
}

Regards,

David


Thursday, May 26, 2016 8:24 PM

Is that supposed to be valid JSON? No matter which JsonConvert methods I try to use, I keep getting the same error:

"Invalid JavaScript property identifier character: (. Path '', line 1, position 2."

I'm not a JSON expert, so I don't know exactly what's wrong with that string, but Newtonsoft.Json doesn't like it.

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Thursday, May 26, 2016 9:04 PM

Hi,

yes BonnieB you'r right, this isn't valid JSON:

{
    g('ads.displayMasterAd',
    {
        adUnit: "/5641/ted2/talk",
        containerId: "master-gpt",
        targeting: {
            "id": 2503,
            "talk": "moran_cerf_this_scientist_can_hack_your_dreams",
            "tag": "brain,communication,future,hack,identity,intelligence,life,mind,science,technology",
            "year": "2016",
            "event": "TED2016"
        }
    });
}

It would be valid if it would e.g. look like this:

{
    "ads.displayMasterAd": {
        "adUnit": "/5641/ted2/talk",
        "containerId": "master-gpt",
        "targeting": {
            "id": 2503,
            "talk": "moran_cerf_this_scientist_can_hack_your_dreams",
            "tag": "brain,communication,future,hack,identity,intelligence,life,mind,science,technology",
            "year": "2016",
            "event": "TED2016"
        }
    }
}

Regards,

David


Friday, May 27, 2016 1:00 AM

David ... that looks a little better (and JsonConvert can Deserialize it to an XmlNode).

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com