L
L
LebedevStr2022-04-06 15:42:13
JSON
LebedevStr, 2022-04-06 15:42:13

How to format JSON into output like this (C#)?

Input

[{"markId":121,"markName":"VW","serieId":300,"serieName":"Multivan","generatedUrl":"category/tandemnyj-nasos-id345-3/vw-id121--2000"},{"markId":121,"markName":"VW","serieId":305,"serieName":"Touareg","generatedUrl":"category/tandemnyj-nasos-id345-3/vw-id121--2000"},{"markId":121,"markName":"VW","serieId":307,"serieName":"Transporter","generatedUrl":"category/tandemnyj-nasos-id345-3/vw-id121--2000"}]


How to get such an output (string)?
VW Multivan, VW Touareg, VW Transporter

Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2022-04-06
@vabka

using System.Text.Json;
var text = Console.ReadLine();
var jsonDocument = JsonDocument.Parse(text);

var elements = from obj in jsonDocument.RootElement.EnumerateArray()
               let mark = obj.GetProperty("markName").GetString()
               let serie = obj.GetProperty("serieName").GetString()
               let name = $"{mark} {serie}"
               select name;

var result = string.Join(", ", elements);
Console.WriteLine(result);

PS: next time ask a little more specific question. For example, "how to parse json" - even in Google you can find the answer to this in the first line.

H
HemulGM, 2022-04-06
@HemulGM

Read the elements of the json array, take the fields of the elements (objects) and add to your list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question