0

Could someone help me. I'm having problem serializing multiple elements.

I'm getting this result which has multiple root elements.

[
  {
    "TITLE": "title1",
    "Total EP": 16,
    "Current EP": 1,
    "URL": "https://www...",
  }
]
[
  {
    "TITLE": "title2",
    "Total EP": 16,
    "Current EP": 2,
     "URL": "https://www...",
  }
]
[
  {
    "TITLE": "title3",
    "Total EP": 16,
    "Current EP": 3,
    "URL": "https://www....",
  }
]

How I can make the result like this? I don't want to have multiple objects. instead I want them merge in the same object.

[
      {
        "TITLE": "title",
        "Total EP": 16,
        "Current EP": 1,
        "URL": "https://www...",
      },
      {
        "TITLE": "title",
        "Total EP": 16,
        "Current EP": 2,
         "URL": "https://www...",
      },
      {
        "TITLE": "title",
        "Total EP": 16,
        "Current EP": 3,
        "URL": "https://www....",
      }
]

This is the code I'm using u

var total = root.Get("total")?.GetInt32();
var title = root.Get("title")?.GetString();
foreach (var url in root.Get("response")?.EnumerateArray()) {
    var currentEp = url.Get("number")?.GetInt32();
    var url = url.Get("url")?.GetString();
    
    var options = new JsonWriterOptions {
        Indented = true
    };

    using (var stream = new MemoryStream()) {
        using (var writer = new Utf8JsonWriter(stream, options)) {
            writer.WriteStartObject();
            writer.WriteString("TITLE", title);
            writer.WriteNumber("Total EP", total);
            writer.WriteNumber("Current EP", currentEp);
            writer.WriteString("URL", url);
            writer.WriteEndObject();
        }
        string json = Encoding.UTF8.GetString(stream.ToArray());
        Console.WriteLine(json);
    }
}

Here's the example of json.

   {
   "pagination":{
      "previous":null,
      "next":null
   },
   "total":10,
   "title": main title,
   "response":[
      {
         "url":"https.....id1", //URL have different ids
         "number":"1",
      },
        {
         "url":"https.....id2",
         "number":"2",
      },
          {
         "url":"https.....id3",
         "number":"3",
      },
          {
         "url":"https.....id4",
         "number":"4",
      },
          {
         "url":"https.....id5",
         "number":"5",
      },
   ]
}

Thank you for the help.

mark123
  • 83
  • 1
  • 5
  • 1
    Does this answer your question? [howto Serialize data consist an array using JsonWriter in C#](https://stackoverflow.com/questions/52417556/howto-serialize-data-consist-an-array-using-jsonwriter-in-c-sharp) – xdtTransform Nov 18 '20 at 14:34
  • related: https://stackoverflow.com/questions/37665240/howto-serialize-a-nested-collection-using-jsonwriter-in-c-sharp/37666080#37666080 – xdtTransform Nov 18 '20 at 14:35
  • I'll take a look. Thanks – mark123 Nov 18 '20 at 14:49
  • Ahm. I'm not using Json.net. I'm using system.text.json – mark123 Nov 18 '20 at 14:50
  • 1
    Same thing. Serialize method work the same and JsonWriter also have the same method and signature. – xdtTransform Nov 18 '20 at 14:55
  • What is the value returned by `root.Get("response")`? Can you please [edit] your question to share the inputs as well as the outputs -- i.e. a [mcve]? – dbc Nov 18 '20 at 16:02
  • @dbc it is an array which have the number and url per element. It's a list of episode – mark123 Nov 18 '20 at 16:15
  • @mark123 - well then might you please [edit] the question to include it, as text? We need that to test any proposed fixes. See [ask], specifically the section *Help others reproduce the problem*. – dbc Nov 18 '20 at 16:23
  • 1
    @dbc already added the json structure – mark123 Nov 18 '20 at 16:36
  • @dbc nevermind. Already fixed and answered :) – mark123 Nov 18 '20 at 16:59
  • Your code has a bunch of other issues, such as `url` being redefined inside the loop, and `"number"` being a string not an integer. I finally was able to get your code to work around the time you accepted the other answer. See https://dotnetfiddle.net/PusUt1. – dbc Nov 18 '20 at 17:01
  • @dbc Thank you so much. I'll try your fix too. I wish I could accept two answers – mark123 Nov 18 '20 at 17:11
  • Thank you so much for your effort. I didn't know you have solution already – mark123 Nov 18 '20 at 17:11
  • Can I accept your answer instead? I find more useful and correction on my code. coudl you post your solution instead? so I can accept as answer – mark123 Nov 18 '20 at 17:14

1 Answers1

0

Try this code

using (var stream = new MemoryStream())
{
    var options = new JsonWriterOptions
    {
        Indented = true
    };
    using (var writer = new Utf8JsonWriter(stream, options))
    {
        writer.WriteStartArray();
        for (int i = 0; i < 10; i++)
        {
            var currentEp = i;
            var total = i;
            var title = $"title{i}";
            var url = $"url:{i}";

            


            writer.WriteStartObject();
            writer.WriteString("TITLE", title);
            writer.WriteNumber("Total EP", total);
            writer.WriteNumber("Current EP", currentEp);
            writer.WriteString("URL", url);
            writer.WriteEndObject();

        }
        writer.WriteEndArray();
        
    }
    string json2 = Encoding.UTF8.GetString(stream.ToArray());
    Console.WriteLine(json2);
}
Stanislav
  • 459
  • 3
  • 6
  • I removed the for loop because I already have existing currentep and urls etc. which is from previous loop. And I'm still getting multiple object :( – mark123 Nov 18 '20 at 15:46
  • you need to replace "for (int i = 0; i < 10; i++)" to "foreach (var url in root.Get("response")?.EnumerateArray())". I added a loop to make the example Common. And variable to – Stanislav Nov 18 '20 at 16:38
  • Wow sorry I didn't analyze. It's now working. thank you so much – mark123 Nov 18 '20 at 16:58
  • How I can move the total ep outside the array? – mark123 Nov 18 '20 at 17:00
  • You need to separate data receiving and mapping. better way add DTO – Stanislav Nov 18 '20 at 17:05