Insert Array into JSON

Asked

Viewed 307 times

0

I have a question, I want to insert a list within a field JSON.

How do I make the strings that are in the list cities become part of the city options for the state Gurajat?

The code is like this:

<!--begin snippet: js hide: false console: true babel: false-->
<!--language: lang-html-->

var countryStateInfo = {
    "USA": {
            "California": {
                "Los Angeles": ["90001", "90002", "90003", "90004"],
                "San Diego": ["92093", "92101"]
            },
            "Texas": {
                "Dallas": ["75201", "75202"],
                "Austin": ["73301", "73344"]
            }
    },
    "India": {
        "Assam": {
            "Dispur": ["781005"],
            "Guwahati": ["781030", "781030"]
        },
        "Gujarat": [

            <%

                List<String> cidades = new List<String>();
                conn.Open();
                cmd.CommandText = "SELECT Distinct [nom_Comunidade/Celula] FROM [Central].[dbo].[TBGC061_CR_tbl_Membros_Mes] where [nom_comunidade/Celula] is not null and [nom_comunidade/Celula] not like '%rede%' and [nom_comunidade/Celula] <> 'Células Simples'";
                dr = cmd.ExecuteReader();
                string x = "";

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cidades.Add(dr.GetString(0));

                    }
                }
                conn.Close();

                foreach (string ct in cidades)
                {
                    x +="'" + ct + "',";
                }
            %>

            <% = x %>

        ]
    }
}

<!-- end snippet -->
  • I advise you to Deserialize json to an object, with the object you can work with the lists, add, remove and after that serialize it to a json string, from a look link, I think it’s a better and safer way

  • @Joãopauloamorim , I doubt what the object would look like. And where would I create it? on the page itself? Can you help me?

  • this json of yours comes from where? this function you’re doing is in the back end? I don’t have much information to help you .

  • My Json is there, on the aspx page. I cannot reference a class I created on the aspx page. All I have is the aspx document. Above what I sent only have check boxes and connection string.

  • I get it, I don’t think I can help you if you get this back-end json for some Rest call or something like that would be easy, because then you could pass the json to Object, work it and then turn it into json again

  • I didn’t understand very well the javascript tag on the question.

Show 1 more comment

1 answer

0

I didn’t quite understand the question, but if I understood correctly you want to play a list in your Json, I recommend Serialize your list in a Json...

JsonConvert.SerializeObject(cidades, Formatting.Indented); This returns you a string already identified, then just play in your Json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.