0

I wanted to read and parse my valid JSON file. but may be i am doing something wrong in loading hence not getting valid file

I have a json file holding valid JSON data. But when i load it with using:

 (StreamReader r = new StreamReader(filePath))
   {
     string json = r.ReadToEnd();
   }

i got something like this:

{\n \"item\": [\n  {\n   \"name\": \"Infinix smart 3 PLUS - 6.2\\\"water drop Display - Triple Rear Camera - 32GB ROM,2GB RAM-Fingerprint sensor-Face unlock\",\n   \"url\": \"https://www.daraz.pk/products/infinix-smart-3-plus-62water-drop-display-triple-rear-camera-32gb-rom-2gb-ram-20ghz-quadcore-processor-i113246358-s1264944281.html?search=1\",\n   \"price\": \"Rs. 17,999\",\n   \"Image\": \"https://static-01.daraz.pk/p/2066cca3b8cf77faa08eaeedfbe69b12.jpg_340x340q80.jpg\",\n   \"Detail\": \"Specifications of Infinix smart 3 PLUS - 6.2\\\"water drop Display - Triple Rear Camera - 32GB ROM,2GB RAM-Fingerprint sensor-Face unlock\\nBrand Infinix SKU 113246358_PK-1264944281 Model Smart 3 Plus\\nWhat’s in the box1xmobile with accessories\\nVIEW MORE\"\n  },\n  {\n   \"name\": \"Honor 7C - 5.99\\\" FHD Display - 3GB RAM - 32GB ROM - Fingerprint Sensor & Face Unlock\",\n   \"url\": \"https://www.daraz.pk/products/honor-7c-599-fhd-display-3gb-ram-32gb-rom-fingerprint-sensor-face-unlock-i3287863-s12693427.html?search=1\",\n   \"price\": \"Rs. 18,999\",\n   \"Image\": \"https://static- .........

now i used this to parse data

    var jobj = JObject.Parse(json);

so i got this kind of result:

{ 
    {
     "item": [
  {
   "name": "Infinix smart 3 PLUS - 6.2\"water drop Display - Triple Rear Camera - 32GB ROM,2GB RAM-Fingerprint sensor-Face unlock",
   "url": "https://www.daraz.pk/products/infinix-smart-3-plus-62water-drop-display-triple-rear-camera-32gb-rom-2gb-ram-20ghz-quadcore-processor-i113246358-s1264944281.html?search=1",
   "price": "Rs. 17,999",
   "Image": "https://static-01.daraz.pk/p/2066cca3b8cf77faa08eaeedfbe69b12.jpg_340x340q80.jpg",
   "Detail": "Specifications of Infinix smart 3 PLUS - 6.2\"water drop Display - Triple Rear Camera - 32GB ROM,2GB RAM-Fingerprint sensor-Face unlock\nBrand Infinix SKU 113246358_PK-1264944281 Model Smart 3 Plus\nWhat’s in the box1xmobile with accessories\nVIEW MORE"
  },
  {
   "name": "Honor 7C - 5.99\" FHD Display - 3GB RAM - 32GB ROM - Fingerprint Sensor & Face Unlock",
   "url": "https://www.daraz.pk/products/honor-7c-599-fhd-display-3gb-ram-32gb-rom-fingerprint-sensor-face-unlock-i3287863-s12693427.html?search=1",
   "price": "Rs. 18,999",
   "Image": "https://static-01.daraz.pk/original/d73129d06b06db79072e07cd132684dd.jpg_340x340q80.jpg",
   "Detail": "Brand Warranty\nScreen: 6.0 inches,IPS LCD capacitive touchscreen,1440 x 720\nMemory: 3GB RAM, 32GB ROM\nProcessor: Octa-core (1.8 GHz Cortex-A53) Qualcomm SDM450 Snapdragon\nCamera: 13 + 2 MP Main Camera, 8 MP Selfie Camera\nBattery:Non-removable Li-Ion 3000 mAh\nOS:Android 8.1 (Oreo)\nSIM: Dual SIM (Nano-SIM, dual stand-by)\nSpecifications of Honor 7C - 5.99\" FHD Display - 3GB RAM - 32GB ROM - Fingerprint Sensor & Face Unlock\nBrand Honor SKU HO688EL0VJIZWNAFAMZ-4039820 Phone Type Smartphone Type of battery Lithium battery Resolution HD Screen Type IPS LCD Processor Type Octa-core Operating system version Android 8.0 Oreo Warranty Policy EN 1-Year Brand Warranty (Inovi Technologies) RAM Memory 3GB Operating System Android Battery Capacity 3000 - 3999 mAh Storage Capacity 32 GB Number of SIM Slots Dual Condition New Camera Front (Megapixels) 8 MP Camera Back (Megapixels) 13 MP Video Resolution 1080p Screen Size (inches) 5.6 - 6 Inch PPI 300-400 PPI Phone Features Touchscreen,GPS,Fingerprint Sensor,Expandable Memory,WiFi,NFC Network Connections GSM,3G,4G\nWhat’s in the boxPhone, Charger, USB Cable, Manual(s), Warranty Card\nVIEW MORE"
  }}

But above loaded json file never gave me results. i have tried even this to parse in my model but it always give me null:

var someType = JsonConvert.DeserializeObject<RootObj>(jobj.ToString());

When i checked on this online JSON validator: https://jsonlint.com/ I found out that i am getting double braces ("{{" and "}}") int he start and end of json file. which is not valid and must be removed. My question is where i am doing wrong why getting double curly braces in start and end although my json file was all correct.

EDIT: I have updated my complete json file which is giving double braces in result. whereas original data is with 1 curly braces in its start and end.

    using (StreamReader r = new StreamReader(icon_path))
    {
        string json = r.ReadToEnd();
        var jobj = JObject.Parse(json);
        List<ObjectList> items = 
   JsonConvert.DeserializeObject<List<ObjectList>>(jobj.ToString());


     }

Above is complete lines for parsing my data in my model

This is my model class

 public class ObjectList
    {
        public string name { get; set; }
        public string url { get; set; }
        public string price { get; set; }
        public string Image { get; set; }
        public string Detail { get; set; }

     } 
Isbah Khan
  • 21
  • 5
  • and if there is any method to remove these braces from start and end , or to replace from double to single thanks – Isbah Khan Jul 02 '19 at 07:34
  • I think that's just the problem with the visualizer you are using to view the contents of `jobj`. Try typing `Console.WriteLine(jobj)` in the [Immediate Window](https://learn.microsoft.com/en-us/visualstudio/ide/reference/immediate-window) and you will see the actual JSON without any visualizer "decoration" -- the extra braces should not be present. – dbc Jul 02 '19 at 07:35
  • my task is to get data inside the json using loop.. like i need to get list having name, url, and other fields – Isbah Khan Jul 02 '19 at 07:37
  • *my task is to get data inside the json using loop..* - that's not what your question asks. You're asking how to get rid of the double-braces which I suspect are an artifact of your data visualizer. If that is your task you can generate an appropriate class and deserialize your JSON to that class. See [This answer to *How do I turn a C# object into a JSON string in .NET?*](https://stackoverflow.com/a/19137100/3744182) and [*How to auto-generate a C# class file from a JSON object string*](https://stackoverflow.com/q/21611674/3744182). – dbc Jul 02 '19 at 07:38
  • Since you are using a `JObject`, you can simply call the `ToString()` override to create your `JSON`. `var jobj = JObject.Parse(json.ToString());` – Rahul Sharma Jul 02 '19 at 07:38
  • Also, can you provide a complete JSON sample -- i.e. a [mcve]? The JSON sample in your question is truncated. – dbc Jul 02 '19 at 07:41
  • sorry Rahul but i am getting the same result in double curly braces :( – Isbah Khan Jul 02 '19 at 07:43
  • @dbc .. yes my question is different but linked with the one i am asking.. when i dont get the required json file how could i loop(which is my actual work).. just want valid parsed json file – Isbah Khan Jul 02 '19 at 07:46
  • Then can you share a [mcve] with code and well-formed JSON that demonstrate the problem? – dbc Jul 02 '19 at 07:48
  • @IsbahKhan Have you tried deserializing the original JSON string that you get: `var someType = JsonConvert.DeserializeObject(json.ToString());` – Rahul Sharma Jul 02 '19 at 08:05
  • yes i have tried, its giving me null value – Isbah Khan Jul 02 '19 at 09:23
  • i am confused,, when my json file is all correct n valid, where m i doing wrong. can anyone help me giving the link of read n parse json data in model , although i have searched and tried alot almost all methods – Isbah Khan Jul 02 '19 at 09:26
  • @IsbahKhan Try using `dynamic` in your case: `dynamic array = JsonConvert.DeserializeObject(json);`. Also can you show us your `string json` ? This is basically happening because your JSON string is incorrectly formed. – Rahul Sharma Jul 02 '19 at 10:52

1 Answers1

0

This is the solution i was looking for.Thanks everyone for your answers.

string jsonetext = File.ReadAllText(icon_path);
Items items = JsonConvert.DeserializeObject<Items>(jsonetext);

I have updated my model classes as below:

public class Daraz
{
    public string name { get; set; }
    public string url { get; set; }
    public string price { get; set; }
    public string Image { get; set; }
    public string Detail { get; set; }

}

public class Items
{
    public List<Daraz> Item { get; set; }
}
Isbah Khan
  • 21
  • 5