nico van vuuren
nico van vuuren

Reputation: 125

C# JsonArray to string (not string array. string only)

I've been searching for this one for quite a while but can't find anything. Appologies then for the title, as there is a lot on converting content to String Arrays, which is not what I need.

I need a way to convert the contents of a JsonArray to string just as is. (Similar to JToken's .ToString()). My scenario is of such a nature that I need the string of an array just as is, irrigardless of the type/s contained within. There are ways of handling weakly typed json with ValueConverters, but I spesifically do not want to use them, as I need to pass the content of a field as a string to a Javascript Function in a WebView.

I have the following json (note markers indicating where string is desired) :

"highlights2":[
  {
    "_id":"highlight2-2850cb68121f9d4093e67950665c45fab02cec81",
    "_rev":"9-c4345794001495104f8cbf5dd6999f3a",
    "content":{             <---- Need this as string
      "#roepman.17.0":[
        [
          233,
          249,
          "itsi-hl-gr"
        ],
        [
          298,
          317,
          "itsi-hl-bl"
        ]
      ],
      "#roepman.19.0":[
        [
          5,
          7,
          "itsi-hl-gr"
        ]
      ]
    },                  <----- Up to here
    "created":1434552587
  }, //...more like this 
],
"book":"book-930d62a2-9b7c-46a9-b092-f90469206900",
"serverTime":1435151280

Ideally I want to parse it into a list of the following type:

public class HighlightInfo2
{
    public string _id { get; set; }
    public string _rev { get; set; }}
    public string content { get; set; }
    public long created { get; set; }
}

However this is not possible, as the content of "content" is of type JsonArray. So to get past not having to specify a type for "content", I use this:

public class HighlightInfo2
{
    public string _id { get; set; }
    public string _rev { get; set; }}
    public Dictionary<string, List<JsonArray>> content { get; set; }
    public long created { get; set; }
}

But this means I still have to at some point convert the List< JsonArray > inside the dictionary to a string as I pass the content of "content" to a Javascript function in a webview later on.

Any way of converting the JsonArray to a string?

Upvotes: 2

Views: 585

Answers (3)

nico van vuuren
nico van vuuren

Reputation: 125

Using a combination of Daniel's Answer and Behzad's Answer, I came up with this class/type for deserialization, which works without a hitch. Thanks for the help.

public class HighlightInfo2
{
    public string _id { get; set; }
    public string _rev { get; set; }
    public long created { get; set; }

    private JToken _content { get; set; }
    public JToken content
    {
        get { return _content; }
        set
        {
            _content = value;
            contentString = JsonConvert.SerializeObject(value);
        }
    }

    [JsonIgnore]
    public string contentString { get; set; }

}

Upvotes: 0

Behzad
Behzad

Reputation: 877

What i suggest is to make another property like contentJson under highlightInfo2 class and put the string of jsonarray in it.

public class HighlightInfo2
{
    private Dictionary<string, List<JsonArray>> _content;
    public string _id { get; set; }
    public string _rev { get; set; }

    public Dictionary<string, List<JsonArray>> content
    {
        get { return _content; }
        set
        {
            _content = value;
            foreach (var item in _content)
            {
                contentJson += string.Join("\r\n", item.Value);
            }
        }
    }
     [JsonIgnore] //note, this depends on your json serializer
    public string contentJson { set; get; }
    public long created { get; set; }
}

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174389

Based on your comment, the formatting of that string is irrelevant. It just has to be valid JSON representing the original data.

What I would suggest then is to make your content member in HighlightInfo2 of type object and simple perform a JsonConvert.SerializeObject(highlightInfo.content) to get the JSON string. This is what you can then pass over to the JavaScript function.

If you need to do this often, you can combine this with Behzad's answer and add another member to your class that stores this converted value.

Upvotes: 3

Related Questions