Loop for add a json for each object in list

iterate list of json objects java
json.parse array of objects
loop through json object
loop through nested json object javascript
json array of objects
json list of lists
json nested arrays
how to get key and value from json array object in javascript

I'm making a tool that needs to export a json. He needs to be in this format:

    {
      "version" : "2",
      "mangas" : [ {
        "manga" : [ "sample", "manganame", 1234567890, 0, 0 ],
        "chapters" : [ {
          "u" : "urlexample",
          "r" : 1
        }, {
          "u" : "urlexample",
          "r" : 1
        }, {
          "u" : "urlexample",
          "r" : 1
        } ]
      } ]
    }

And this is my code:

    void createJson(String manganame, String mangaoid, String sourceid)
            {
                String[] mangainfo = { "/manga/" + mangaoid, manganame, sourceid, "0", "0" };



                var root = new RootObject()
                {
                    version = "2",
                    mangas = new List<Manga>()
                    {
                        new Manga()
                        {

                            manga = mangainfo,
                            chapters = new List<Chapter>()
                            {
                                new Chapter
                                {

                                    u = "sample",
                                    r = 1

                                }
                            }

                        }

                    }

                };


                var json = JsonConvert.SerializeObject(root);
                File.WriteAllText(@"D:\path.txt", json);
                Console.WriteLine(json);


            }

I'm lost, if someone can help me. Already give a search on Google, but the answer didn't come up in my head, already trying for a few time, slowly I'm getting but now is time to ask for help lol

For the list I was talking about, I'll explain it. I have a sqlite DB that have various information from mangas etc... I execute a query where I filter by a id, "SELECT * FROM MangaChapter WHERE manga_id = 'someid'", then i put the result on a list using a for loop. In the DB chapter url is stored like that "mr-chapter-166165" this is why i have had to concat string in chapterList.add.

   List<String> chapterList = new List<String>();

    cmd.CommandText = "SELECT * FROM MangaChapter WHERE manga_id = '3252'";
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        chapterList.Add("/pagesv2?oid=" + reader.GetString("oid"));
    }

For reference this is what I'm using to manage the sqlite db https://www.nuget.org/packages/dotConnect.Express.for.SQLite/

In the list, each chapter is something like that "/pagesv2?oid=mr-chapter-166165", if I print all the list on the console we'll be having something like that:

    /pagesv2?oid=mr-chapter-166165
    /pagesv2?oid=mr-chapter-166166
    /pagesv2?oid=mr-chapter-166167

Here are the classes I generated from the given JSON sample

public class Chapter
{
    [JsonProperty("u")]
    public string U { get; set; }

    [JsonProperty("r")]
    public int R { get; set; }
}

public class Manga
{
    [JsonProperty("manga")]
    public IList<object> MangaInfos { get; set; }

    [JsonProperty("chapters")]
    public IList<Chapter> Chapters { get; set; }
}

public class Example
{
    [JsonProperty("version")]
    public string Version { get; set; }

    [JsonProperty("mangas")]
    public IList<Manga> Mangas { get; set; }
}

and here the code to reproduce the give JSON sample

var d = new Example
{
    Version = "2",
    Mangas = new List<Manga>
    {
        new Manga()
        {
            MangaInfos = new List<object>{ "sample", "manganame", 1234567890, 0, 0 },
            Chapters = new List<Chapter>
            {
                new Chapter()
                {
                    U = "urlexample",
                    R = 1,
                },
                new Chapter()
                {
                    U = "urlexample",
                    R = 1,
                },
                new Chapter()
                {
                    U = "urlexample",
                    R = 1,
                },
            },
        },
    },
};

var json = JsonConvert.SerializeObject(d,Formatting.Indented);
Console.WriteLine(json);

The output looks like

{
  "version": "2",
  "mangas": [
    {
      "manga": [
        "sample",
        "manganame",
        1234567890,
        0,
        0
      ],
      "chapters": [
        {
          "u": "urlexample",
          "r": 1
        },
        {
          "u": "urlexample",
          "r": 1
        },
        {
          "u": "urlexample",
          "r": 1
        }
      ]
    }
  ]
}

and live view at .net fiddle

JSON Objects, JSON objects are written in key/value pairs. Each key/value pair is separated by a comma. You can loop through object properties by using the for-in loop:  Note To loop over a object array in JSON formatted string, you need to converts it to JavaScript object (with JSON.parse() or $.parseJSON()) before parse it with jQuery $.each(). See this example – JQuery Loop Over JSON String […]

Based on your comment, if you want to have various chapters for each "Manga", you have to change your data structure and that changes the Result Json you want. maybe something like this?

public partial class Root
{
    public long Version { get; set; }
    public Mangas[] Mangas { get; set; }
}

public partial class Mangas
{
    public Manga[] Manga { get; set; }

}

public partial class Chapter
{
    public string U { get; set; }
    public long R { get; set; }
}

public partial struct Manga
{
    public long? Integer;
    public string String;
    public Chapter[] Chapters { get; set; }

}

JSON Arrays, In JSON, array values must be of type string, number, object, array, boolean or null. To access arrays inside arrays, use a for-in loop for each array:  The way you would solve this is by adding a new to the subResult1 object. I don't know what type of object you have because it's not included in the sample, but let's say that above you have this: Map<String, String> subResult1 = new HashMap<String, String>(); In your code you would need to add this: //subResult - layer2 subResult1 = new HashMap();

Iterate through chapters. Solution below.

    class Parent
    {
        public int Version { get; set; }
        public List<Manga> mangas { get; set; }
    }

    class Manga
    {

        public List<object> manga { get; set; }
        public List<Chapter> chapters { get; set; }

    }

    class Chapter
    {

        public  string u { get; set; }
        public int r { get; set; }

    }

    void createJson(String manganame, string mId, String mangaoid, long sourceid)
    {

        var json = new Parent()
        {
            Version = 2,
            mangas = new List<Manga>()
            {
                new Manga()
                {
                    manga = new List<object>{ "/manga/"+mangaoid, manganame, sourceid, 0, 0 },
                    chapters = Chapters(),
                }
            }
        };

        var sjson = JsonConvert.SerializeObject(json, Formatting.Indented);
        File.WriteAllText(@"C:\Users\Izurii\Desktop\oi.json", sjson);

    }

    List<Chapter> Chapters()
    {
        List<Chapter> chapters = new List<Chapter>();
        for(int i = 0; i < links.Count; i ++)
        {
            chapters.Add(
            new Chapter()
            {
                u = links[i],
                r = 1,
            });

        }
        return chapters;
    }

Looping JavaScript Arrays Using for, forEach & More ‍ , JavaScript for loops iterate over each item in an array. The node list object does have a forEach method that functions the exact same as the Array And forEach is an abstraction and they almost always add overhead. Step 2 – Loop through every object in our JSON object. Next step is to create a simple loop. We can then get every object in our list of JSON object and append it into our main div. for (var i = 0; i < data.length; i++) { // append each person to our page } Step 3 – Append each person to our HTML page. Inside the for-loop we will append

JSON forEach tutorial, JSON forEach tutorial shows how to loop over a JSON array in JavaScript. The json-server is a JavaScript library to create testing REST API. First, we create a project directory We go over the entries of each object and print the key and the value to the console. List all JavaScript tutorials. Home Top of  for (var name in json_parsed) { console.log(name + "=" + json_parsed[name]); } If you need to check if the corresponding property is defined on the object in question, not on some of those on the prototype chain (this is ridiculous for the case in question, but still useful), you can add this check:

JavaScript iterate through object keys and values (Example), Why aren't you passing the corresponding object to JSON.stringify ? over 1 year ago ·. Add a Variables-> "Initialize variable" action, Name set to JsonArray, Type set to Array and Value set to the json data that you provided. Add a "Apply to each", input parameter set to JsonArray variable. Within "Apply to each", add a Compose action, Inputs set to following formula:

Master the art of looping in JavaScript with these incredible tricks, In the below code, I am looping through all the numbers in an array, and printing each of JSON is a very popular format to transmit data objects consisting of attribute–value pairs and array data types. Next, add the CSS for giving proper design to this html table: Python List Append What is Chromium? I needed to add items to JSON object in a for loop. So this works too thanks events[i] = checks[i].value; // assuming [i] is the iterator console.log(events.json); // prints something like Object

Comments
  • My bad, already corrected it.
  • mangainfo needs to be an object[] containing 2 strings and 3 int
  • Okay. Aside from that, my code actually generate some json. I'm gonna five more context, the json generated needs to contain the manga name, source id etc... this is working well. Now my problem is that the json needs to contain the chapters released, regard in this part "new Chapter { u = "", r=1 }" each of this add one chapter to JSON. Here lies the problem, the chapters list is a List, and each manga have various chapters. So I was thinking if I have some way to "foreach chapter" the code gives a "new Chapter...."
  • Yeah. That's pretty much the output that's needed. Now a problem, exactly that part "new Chapter() { U = "urlexample", R = 1, }" where you need to repeat that for add another chapter. We have some way to get that in a loop (foreach, for..)?. Example, a certain manga have 700 chapters, it's not something that you want to write, supposing we have 3 "Mangas" and each 300 chapters. Let's think about, if I have a List with the url of all the chapters, how can i make "new Chapter() { U = "urlexample", R = 1, }" for each chapter without manually typing. Thank's for the replies.
  • I do not know how your list looks like. How can I show you how to build this with your list I do not know?
  • Very sorry for that. My mistake. I already edited the post, if you wanna something else just say it.
  • Well, that is a list of strings, but how do we get the informations we need to fill the chapter list?
  • Yes. I retrieve the information from a sqlite db, I just edited posting the code about that.