1

I have the following array

[
 {"item":"pageTitle", "value":"My Page Title"},
 {"item":"instructionalText", "value":"My Instruction Text"},
 {"item":"continueButton", "value":"Continue"}
]

And I want to convert to something like this? Maybe?

{"pageTitle":"My Page Title", "InstructionalText":"My Instructional Text", etc...}

My end goal is to be able to reference it like myContent.pageTitle and get My Page Title in return.

My Model:

    public class GiftAidContent
{
    [JsonPropertyAttribute("PageTitle")]
    public string PageTitle { get; set; }
    [JsonPropertyAttribute("InstrText")]
    public string InstrText { get; set; }
    [JsonPropertyAttribute("AcceptBtnText")]
    public string AcceptBtnText { get; set; }
    [JsonPropertyAttribute("DeclineBtnText")]
    public string DeclineBtnText { get; set; }

}

Using the dictionary as idle_mind suggested in comment, I now have this.

content
Count = 4
[0]: {[page_title, Gift Aid]}
[1]: {[instructional_text, Lorem ipsum dolor sit amet.]}
[2]: {[continue_btn, Continue]}
[3]: {[cancel_btn, Cancel]}

Source that has current data.

List<GlobalContent> listOfGlobalContentsWork 

    public class GiftAidObject
{
    [JsonPropertyAttribute("isGiftAidEligible")]
    public bool isGiftAidEligible { get; set; }    
    public List<GlobalContent> GlobalContentList { get; set; }
    public string ErrorText { get; set; }
}
Marty
  • 65
  • 2
  • 9
  • You're looking for a `Dictionary`. – Idle_Mind Feb 17 '22 at 03:07
  • Show us that "array", though, that you "have"...with a colon in it?... – Idle_Mind Feb 17 '22 at 03:08
  • Almost have it the Dictionary that you offered. It is looking like this now. – Marty Feb 17 '22 at 03:18
  • @Idle_Mind OP *probably* looking for more of "expando" (https://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c) object. but indeed, it is quite unclear what they have as source and what is expected resulting object – Alexei Levenkov Feb 17 '22 at 03:18
  • content Count = 4 [0]: {[page_title, Gift Aid]} [1]: {[instructional_text, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque gravida lorem sed blandit convallis. Curabitur et nunc ac felis sodales imperdiet vel quis elit. Aenean id porta ipsum. Aenean bibendum mi nisi, sit amet lacinia orci mollis in. Aliquam in ligula nec lacus interdum eleifend at eu augue. Sed vel massa bibendum, semper nibh vitae, sagittis urna. Donec cursus nibh non diam rhoncus imperdiet.]} [2]: {[continue_btn, Continue]} [3]: {[cancel_btn, Cancel]} – Marty Feb 17 '22 at 03:18
  • This is my model that I am trying to put it into. 'code' public class GiftAidContent { [JsonPropertyAttribute("PageTitle")] public string PageTitle { get; set; } [JsonPropertyAttribute("InstrText")] public string InstrText { get; set; } [JsonPropertyAttribute("AcceptBtnText")] public string AcceptBtnText { get; set; } [JsonPropertyAttribute("DeclineBtnText")] public string DeclineBtnText { get; set; } } – Marty Feb 17 '22 at 03:20
  • @Marty you should [edit] the question if you have details to add to the question or *answer* (as separate answer) if you have an answer (even if it is your own question). Also for later I'd recommend still to [edit] the question to clarify what you have/want. – Alexei Levenkov Feb 17 '22 at 03:20
  • @AlexeiLevenkov Will do. Thanks!! – Marty Feb 17 '22 at 03:22

1 Answers1

0

It would be easier doing this using dynamic instead of your model:

    var json = "[{\"item\":\"pageTitle\", \"value\":\"My Page Title\"},{\"item\":\"instructionalText\", \"value\":\"My Instruction Text\"},{\"item\":\"continueButton\", \"value\":\"Continue\" }]";

    var myObject = new ExpandoObject() as IDictionary<string, Object>; 
    dynamic foo = JArray.Parse(json);
    foreach (JObject obj in (JArray)foo) { 
        
        
        string item = obj.Properties().Where(x => x.Name == "item").Select(x => x.Value).First().ToString();
        string valu = obj.Properties().Where(x => x.Name == "value").Select(x => x.Value).First().ToString();
        
        myObject.Add(item, valu);
        
    }
    string newJson = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
    Console.WriteLine(newJson);
Amit
  • 126
  • 2
  • 7