Home > Software engineering >  C# sending json data into multidimentional array
C# sending json data into multidimentional array

Time:06-18

In a Xamarin project I am trying to post data to Lumen API. The API takes this data:

item[0][ProductId] = 1

item[0][ProductName] = Test

and so on. The idea here is that there can be more than one productline that will be added to an invoice by the api.

The challenge here is now how to add the item[i] before the product.

Here is the productDTO:

 product = new ProductDto
                {
                    ProductId = i.ProductId,
                    ProductName = i.ProductName,
                    Price = i.Price,
                    Quantity = i.Quantity,
                    TaxPercetage = i.TaxPercetage,
                    Total = i.Total,
                    TotalTaxes = i.TotalTaxes
                };

And then finally posting to the API

var url = "https://localhost/Lumen/registerSale";
                using var client = new HttpClient();
                string token = Preferences.Get("token", "");
                string jsonstringremake = "";
                var request = new HttpRequestMessage(HttpMethod.Post, url);
                var json = JsonConvert.SerializeObject(product);

This code give me this json encoded data: "{"ProductId":1,"ProductName":"Coca Cola","Quantity":1.0,"Price":3.0,"TaxPercetage":25,"Total":3.0,"TotalTaxes":0.75}"

The question is, how can I create a function that give me this json data: "{"item[0]ProductId":1,"item[0]ProductName":"item[0]Coca Cola","item[0]Quantity":1.0,"item[0]Price":3.0,"item[0]TaxPercetage":25,"item[0]Total":3.0,"item[0]TotalTaxes":0.75}" where item is item[i]?

CodePudding user response:

I ran your expected JSON through a linter & I was actually quite surprised it is valid. However, when it was formatted a bit better I saw why I thought it was odd.

First keep in mind that JSON is just name/value pairs. In your case you have names which include array-type brackets [].

{
  "item[0]ProductId": 1,
  "item[0]ProductName": "item[0]Coca Cola",
  "item[0]Quantity": 1,
  "item[0]Price": 3,
  "item[0]TaxPercetage": 25,
  "item[0]Total": 3,
  "item[0]TotalTaxes": 0.75
}

Basically, this means that you have a string name which includes array brackets. For example : item[0]ProductId

Well, to get the one named item[1]ProductId you it is going to be odd because the index value is a part of the name.

You'd have to iterate over the names, inserting the appropriate index values or something.

I agree with comment which said you probably need a List<productDTO>

List<ProductDTO> allProducts = new List<ProductDTO>();

product = new ProductDto
                {
                    ProductId = i.ProductId,
                    ProductName = i.ProductName,
                    Price = i.Price,
                    Quantity = i.Quantity,
                    TaxPercetage = i.TaxPercetage,
                    Total = i.Total,
                    TotalTaxes = i.TotalTaxes
                };
allProducts.Add(product);

// Keep adding products

JsonConvert.SerializeObject(allProducts);

CodePudding user response:

try this

var jsonParsed=JObject.FromObject(product);

var newJObject= new JObject();

var prefix="Item[0]";

foreach (var prop in jsonParsed)
    newJObject.Add(prefix prop.Key,prop.Value);

 var json = newJObject.ToString();

new json

{
  "Item[0]ProductId": 1,
  "Item[0]ProductName": "Coca Cola",
  "Item[0]Quantity": 1.0,
  "Item[0]Price": 3.0,
  "Item[0]TaxPercetage": 25,
  "Item[0]Total": 3.0,
  "Item[0]TotalTaxes": 0.75
}
  • Related