4

I have a JSON and part of it contains a string values like

 "parent_crumbs": [
    "Platforms",
    "STATS , EXPE , ESTAP",
    "Portal"
],

I use the below code to read the value from the JSON object

 JObject _task; //parse the JSON to JOBJECT first               
 string values=    (string)_task["parent_crumbs"].ToString();

This will return a string with square brackets and all quotes only.

How can i convert this to a string array in C#

I can remove [ and ] and then split on , But when , is present in string items middle the entire assumptions will break. So is any methods availabe to read from JArray to String Array

4
  • Which version of json.net/Newtonsoft.Json do you use? Commented Oct 19, 2018 at 9:08
  • Latest version from NUGET Commented Oct 19, 2018 at 9:11
  • 1
    Try IEnumerable<string > strings= _task[“parent_crumbs”].Select(s=>s.Value<string>(); Commented Oct 19, 2018 at 9:23
  • Could you resolve your issue? Commented Oct 22, 2018 at 7:08

1 Answer 1

5

You didn't post the full json string, so let's assume it's something like this:

{
    "id": 123,
     "parent_crumbs": [
        "Platforms",
        "STATS , EXPE , ESTAP",
        "Portal"
    ]
}

You can create a C# model that matches this structure as follows:

public class Data
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("parent_crumbs")]
    public List<string> ParentCrumbs { get; set; }
}

And then deserialize the json string into an instance of a Data class:

string json = @"{
    ""id"": 123,
     ""parent_crumbs"": [
        ""Platforms"",
        ""STATS , EXPE , ESTAP"",
        ""Portal""
    ]
}";

Data data = JsonConvert.DeserializeObject<Data>(json);

foreach (string crumb in data.ParentCrumbs)
{
    Console.WriteLine(crumb);
}

EDIT

Instead of deserializing the whole json string you can do the following:

string json = @"{
    ""id"": 123,
     ""parent_crumbs"": [
        ""Platforms"",
        ""STATS , EXPE , ESTAP"",
        ""Portal""
    ]
}";

JObject data = JObject.Parse(json);
JToken crumbsToken = data.GetValue("parent_crumbs");
List<string> crumbs = JsonConvert.DeserializeObject<List<string>>(crumbsToken.ToString());

// alternative way to get the string values, as suggested by @BrianRogers
List<string> crumbs = crumbsToken.ToObject<List<string>>();
Sign up to request clarification or add additional context in comments.

6 Comments

Its a very length y JSON and building model for all properties is not practical for me . Around 60+ properties i have to make. Thats why i am doing inline conversion to string or JArray
@JibinMathew I added more details to my answer, please take a look.
On deserialization, note that you can create a model with fewer properties and it will work anyway.
@SandRock that's cool, I never tried it TBH - thanks for the info :-)
@RuiJarimba Don't convert the JToken to string just to turn around and deserialize it. Instead, use .ToObject<T>() like this: List<string> crumbs = crumbsToken.ToObject<List<string>>();
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.