0

All the solutions that I have found to this, not satisfied with me, because I do not have JSON same structure. My JSON array:

string str = @"[{
    "type": "text",
    "field": [
        "tags",
        "track_title"
    ],
    "value": "hero",
    "operation": "must"
},  {
    "type": "range",
    "field": "duration",
    "value": [
        0,
        5
    ],
    "operation": "must"
}, {
    "type": "range",
    "field": "duration",
    "value": [
        180,
        null
    ],
    "operation": "must"
}]"

As you can see, JSONs are different. So I can't using specific model class for convert via it. I need to receive List<string> with separate JSON. How can I parse it ?

3
  • 1
    What is the value of each element in your List<string>? Commented Feb 14, 2016 at 17:03
  • @AnhTriet Just separate JSONs from array in string view. Commented Feb 14, 2016 at 17:06
  • You mean, you want to separate each object in the JSON array? Not converting them into object? Just leave them as string? Commented Feb 14, 2016 at 17:14

2 Answers 2

1

Base on what I understand, I suggest we do this manually. Here is the code:

            string str = @"[{
    'type': 'text',
    'field': [
        'tags',
        'track_title'
    ],
    'value': 'hero',
    'operation': 'must'
},  {
    'type': 'range',
    'field': 'duration',
    'value': [
        0,
        5
    ],
    'operation': 'must'
}, {
    'type': 'range',
    'field': 'duration',
    'value': [
        180,
        null
    ],
    'operation': 'must'
}]";
            // Remove 2 brackets []
            str = str.Remove(0, 1);
            str = str.Remove(str.Length - 1, 1);

            // Split string
            string[] delimiter = {"},"};
            string[] data = str.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

            // The list you want
            List<string> result = new List<string>();

            // Turn the array into List with some modification
            foreach (string s in data)
            {
                string tmp = s;
                if (!s.EndsWith("}"))
                {
                    tmp = s + "}";
                }
                result.Add(tmp.Trim());
            }

            // Display data
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }

The code is self-explained. Please let me know if you find something unclear.

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

1 Comment

Thank you, It works perfect ! I will use your split method. But actually when I wrote this question, i supposed that should be any default methods in JSON-helpers library. But you answ is ok. Thank you a lot
1

Basically what you can do is deserialize Json to a dynamic type so it can handle the differences. For example here you have how to do it with Json.Net library

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.