Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-9147

Simplify Arrays as JSON strings Code Snippet

    XMLWordPrintable

Details

    • Task
    • Resolution: Resolved
    • Major
    • Mobile 3.0
    • Mobile 3.0
    • Mobile
    • None

    Description

      Reference: https://ibsoln.github.io/stage/stage300/couchbase-lite/current/csharp/document.html

      The current code snippet creates MutableArrayObject from JSON and then loop through the array to create a document from each item of the array. I think it's better and more simplify to just create one doc using the array object.

      For Example:

       

      Database dbNew = new Database(ourdbname);
       
      // JSON String -- an Array (3 elements. including embedded arrays)
      var thisJSONstring = "['Hotel Ned', 'Hotel Ted']".Replace("'", "\"");
       
      // Create mutable array using JSON String Array
      var hotels = new MutableArrayObject(json: thisJSONstring);
       
      // Create a document with the featured hotels array object
      var newdoc = new MutableDocument("1002"); 
      newDoc.SetArray("features", hotels)
      dbNew.Save(newdoc);
       
      // Get document and access the featured hotels
      var doc = dbNew.GetDocument("1002");
      var features = doc.GetArray("features");
      foreach (string feature in features) {
          System.Console.Write(feature);
      }
       
      // Convert features hotels into JSON
      var featuresJSON = features.ToJSON();
      

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Ian.bridge Ian Bridge
            pasin Pasin Suriyentrakorn
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty