Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

c# turn string into an object (deserialize)

hi, I have the attached text file.  I can't change it's format in any way.  I need to turn it into a c# object (like a list).  How do I do this?


the file will show a representation of this:
{
    "totalpages": "2",
    "currpage": "1",
    "totalrecords": "1012",
    "filteredrecords": "None",
    "recordcount": "1012",
    "rows": [
        {
            "GroupByDesc": "",
            "Name": "The Name<br> 2701 Ming Avenue",
            "Address": "2701 Ming Avenue",
            "City": "Bakersfield",
            "State": "California",
            "Country": "United States of America",
            "IndexRecordNum": null,
            "LatestInspDate": "",
            "CbiId": "277605"
        },
        {
            "GroupByDesc": "",
            "Name": "The Name<br> 1233 W Avenue P",
            "Address": "1233 W Avenue P",
            "City": "Palmdale",
            "State": "California",
            "Country": "United States of America",
            "IndexRecordNum": null,
            "LatestInspDate": "",
            "CbiId": "277607"
        }
    ]
}

GetLocationSelectorGridData.txt
0
jackjohnson44
Asked:
jackjohnson44
  • 2
  • 2
1 Solution
 
fritzfrancisCommented:
This is a Json string. You can use the .NET Framework's DataContractJsonSerializer or you can use a third-party library like NetwonSoft or CodeTitans.Json.
0
 
jackjohnson44Author Commented:
Hi, yes it is  Json string.  I have been having issues getting an answer to this question.

People have mentioned using that before but I could not get it to work.  I also can't use thrid party tools.  I don't have access to anything but the string in the file that I attached.  I can't change the way that I get it, the data contract, or anything else.  It is a blackbox web service and for my purposes it isn't important that it originally came from a webservice.  I have a long string as input (the text file).

I need a function that will behave like this:
private static object DoSomethign(string myInputString)

I appreciate everyone's help, but I really need a solution with code.  I have been working on this for a long time and am not making progress.  Thanks!
0
 
jackjohnson44Author Commented:
More specifically:

private static Dictionary<string, string> DoSomethign(string myInputString)

0
 
fritzfrancisCommented:
I have attached code that reads your file and parses it. I am using Microsoft's DataContractJsonSerializer. This satisfies your requirement of no third-party libraries. This class is in the System.Runtime.Serialization assembly in the Json namspace. I had to remove extra "\" chanracters that you had in your file. I tried submitting my Visual Studio solution to you in a .zip format, but EE kept rejecting it. So you can build a console application yourself and paste to code I submitted over Program.cs. Then put the file I attached in the bin\Debug directory. Finally add a reference to the System.Runtime.Serialization assembly. I hope this helps.
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;

namespace JsonDeserializationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string json = File.ReadAllText("GetLocationSelectorGridData.txt");
                Console.WriteLine("JSON String ... ");
                Console.WriteLine(json);
                Console.WriteLine();

                Console.WriteLine("JSON Parsed ... ");
                Class1 class1Instance = ParseJSON(json);
                Console.WriteLine("currpage = {0}", class1Instance.currpage);
                Console.WriteLine("recordcount = {0}", class1Instance.recordcount);
                Console.WriteLine("totalpages = {0}", class1Instance.totalpages);
                Console.WriteLine("totalrecords = {0}", class1Instance.totalrecords);
                Console.WriteLine("rows.Length = {0}", class1Instance.rows.Length);
                Console.WriteLine();
                for (int index = 0; index < class1Instance.rows.Length; index++)
                {
                    Console.WriteLine("rows[0].Address = {0}", class1Instance.rows[index].Address);
                    Console.WriteLine("rows[0].CbiId = {0}", class1Instance.rows[index].CbiId);
                    Console.WriteLine("rows[0].City = {0}", class1Instance.rows[index].City);
                    Console.WriteLine("rows[0].GroupByDesc = {0}", class1Instance.rows[index].GroupByDesc);
                    Console.WriteLine("rows[0].IndexRecordNum = {0}", class1Instance.rows[index].IndexRecordNum);
                    Console.WriteLine("rows[0].LatestInspDate = {0}", class1Instance.rows[index].LatestInspDate);
                    Console.WriteLine("rows[0].Name = {0}", class1Instance.rows[index].Name);
                    Console.WriteLine("rows[0].State = {0}", class1Instance.rows[index].State);
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Press ANY key to exit ...");
            Console.ReadKey();
        }

        private static Class1 ParseJSON(string json)
        {
            Class1 class1Instance = null;
            using (StringReader reader = new StringReader(json))
            {
                using (MemoryStream stream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(json)))
                {
                    DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(Class1));
                    object o = serializer.ReadObject(stream);
                    class1Instance = (Class1)o;
                }
            }
            return class1Instance;
        }
    }

    public class Class1
    {
        public int totalpages { get; set; }

        public int currpage { get; set; }

        public int totalrecords { get; set; }

        public int recordcount { get; set; }

        public Class2[] rows { get; set; }
    }

    public class Class2
    {
        public string GroupByDesc { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public int? IndexRecordNum { get; set; }

        public string LatestInspDate { get; set; }

        public int CbiId { get; set; }
    }
}

Open in new window

GetLocationSelectorGridData.txt
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now