2

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?

2
  • 1
    Aren't you trying to work around some other problem that exists elsewhere? Why do you need that content as string? I would say: If it was meant to be a string, it would be. Commented Jun 25, 2015 at 7:42
  • I pass the string onto a JavaScript function, this function takes the string, stores it and when needed, parses is in JS for further use. Commented Jun 25, 2015 at 8:25

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Came here to say exactly this :) Shortly after Daniel Hilgarth's comment yesterday I realized I was being silly, and that I can just Serialize the json, and store it as is. I now make the content member of type JToken. Combining it with Behzad's answer, I make a contentString member, which does the conversion. Finally adding a Scriptignore attribute to the contentString field so as to not serialize it later on.
Just one question remaining then. Any reason you chose an Object type? Is using JToken better/worse? (imo having any other type than Object is usually better) - Keep in mind, the whole highlight2 object is saved in a SQLite database at the end of the day.
@nicovanvuuren: My thinking was: This member is an intermediate step that you don't actually want to use. Making it object communicates this - at least a little bit. From a technical point of view, using JToken will work just as well.
2

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; }
}

3 Comments

This is a good idea in general, but your concrete code won't generate valid JSON. You should be using facilities that generate valid JSON. See my answer for an example.
Yes, you are right, simply by adding the [JsonIgnore] or [ScriptIgnore] the model will be valid, right ?
No. What I mean is that the value of contentJson will not have the expected value. After fixing the errors in the code, mainly JArray instead of JsonArray and contentJson += instead of contentJson = , the result of contentJson will only be the array representations without the property names.
0

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; }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.