0

I've some JSON data coming back as a string that looks like (copy and paste from VS text visualizer):

{
   "error":0,
   "result":{
      "name":"wjetestuser1",
      "id":"0eu0_User_2_0b4cfb616e648d4792056c1a6e7d801e_null",
      "status":"ACTIVE"
   }
}
{
   "match":[
      [
         "domain.id",
         "=",
         "2"
      ],
      [
         "loginName",
         "=",
         "wjetestuser1"
      ]
   ],
   "return":[
      "name",
      "id",
      "status"
   ]
}

I'm trying to turn this into a List for everything after the "result": and before {"match": without using a replace command, so I'll end up with a list that looks something like:

Name, wjetestuser1
id, 0eu0_User_2_0b4cfb616e648d4792056c1a6e7d801e_null
status, ACTIVE

If I can get the error code status back thats a bonus, but really not needed.

I'm hoping there is a simple one (or a few liners) that don't involve hacking the string apart with a replace regex command.

Various code attempts so far, but this worked for me if I strip before and including "result": and after and including {"match":

s below is the output above as a single line

s = commonCode.ExeApiCall(url);
var DSData = new List<KeyValuePair<string, string>>();
var jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(s);

errors at the 3rd line down with error:

Additional text encountered after finished reading JSON content: {. Path '', line 1, position 119.

11
  • 5
    There's a million JSON to C# questions on Stack Overflow, have you tried looking? Commented Aug 3, 2017 at 15:26
  • 2
    So why didn't you show us some of your effort? Because right now your question looks like "please write code for me" which we don't do here. Commented Aug 3, 2017 at 15:28
  • 3
    why you have 2 root elements? Commented Aug 3, 2017 at 15:33
  • 2
    that position 119 from error message is the last parenthesis of the first root element. You have to split that string into two json objects. (i wonder if it is a bug in their API or they expect you to do extra work parsing string of multiple json objects). Commented Aug 3, 2017 at 15:40
  • 2
    I knew I had saw this before: From Json.NET documentation Read Multiple Fragments With JsonReader Commented Aug 6, 2017 at 7:35

1 Answer 1

1

As Sir Rufo pointed out in this comment if you use Newtownsoft library you can Read Multiple Fragments With JsonReader. Example from the website:

public class Role
{
    public string Name { get; set; }
}

public class Main
{
    string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";
    IList<Role> roles = new List<Role>();

    JsonTextReader reader = new JsonTextReader(new StringReader(json));
    reader.SupportMultipleContent = true;

    while (true)
    {
        if (!reader.Read())
            break;

        JsonSerializer serializer = new JsonSerializer();
        Role role = serializer.Deserialize<Role>(reader);
        roles.Add(role);
    }

    foreach (Role role in roles)
        Console.WriteLine(role.Name);
}

My original answer was making minimal JSON parser to this particular case scenario. See revision history for that.

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

5 Comments

Thank you. the top set of code worked straight off. Second set, I put my string 's' between the brackets of .ToJsons(s) and it came out perfect. This was a lot tidier than the regex mess I was working with.
@WayneEvans fyi friend: it's impossible to solve it with regex. you have to use stack. some reference to study
Try your extension method with this valid JSON string {"foo}{5":"bar}{6"}
@alex You have to write a (nearly) complete json parser to split the root elements, which can be an object or an array as well. If you are going to write one, I can give you some JSON examples for the unit tests :o)
@alex No need to implement a feature already implemented by Json.NET (see my last comment at the question)

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.