Add image path in json file Xamarin.forms

Elkin Espiritu 41 Reputation points
2021-05-01T12:39:24.19+00:00

how to add local image path in json file in xamarin.forms?

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-05-03T04:10:05.72+00:00

    Hello,

    Welcome to Microsoft Q&A!

    1. Read the json content from file .
    2. Deserialize the content into object .
    3. Add a new property named image in model and assign the value to it .
    4. Serialize the object into json string .
    5. Write the string into file .

    Model

       public class RootObject  
           {  
               public string name { get; set; }  
               public string id { get; set; }  
               public string image { get; set; }  // add this property  
           }  
    

    Original json data

       {"name":"cole" , "id" : "123"}  
    

    Handling

       var file = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test.json");  
         
                   if (File.Exists(file))  
                   {  
                       string text = File.ReadAllText(file);  
         
                       RootObject obj = JsonConvert.DeserializeObject<RootObject>(text);  
         
                       obj.image = "https://us.v-cdn.net/5019960/uploads/userpics/468/p1NMCBY2PV254.jpeg";  
         
                       string json = JsonConvert.SerializeObject(obj);  
         
                       File.WriteAllText(file, text);  
                   }  
    

    (Embedding resource does not support writing operation , so we can't use it . )

    Best Regards,
    Cole Xia


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.