0

I create application ASP.NET Core MVC 2 for show markers on map. I create Model and Controller.

Model:

    public class MarkersModel
{

    public string title { get; set; }

    public double lat { get; set; }

    public double lng { get; set; }

}

Controller:

    public class MarkersController : Controller
{
    public IActionResult Index()
    {
        return View("Markers");
    }

    public IActionResult GetMarkers()
    {
        //landmarks
        var model1 = new MarkersModel {lat = 53.43382, lng = 14.55559, title = "Galaxy"};
        var model2 = new MarkersModel { lat = 53.42800, lng = 14.55124, title = "Kaskada" };

        //buildings
        var model3 = new MarkersModel { lat = 53.43263, lng = 14.55436, title = "Zig-Zag" };
        var model4 = new MarkersModel { lat = 53.43241, lng = 14.55568, title = "Baltica" };

        //stops
        var model5 = new MarkersModel {lat = 53.43269, lng = 14.54787, title = "Plac Grunwaldzki" };
        var model6 = new MarkersModel { lat = 53.43186, lng = 14.55485, title = "Plac Rodła"};

        //others
        var model7 = new MarkersModel { lat = 53.43133, lng = 14.55519, title = "KFC" };
        var model8 = new MarkersModel { lat = 53.43172, lng = 14.55384, title = "Kurczak z rożna"};

        //MarkersModel.
        return Json(new {model1, model2, model3, model4, model5, model6, model7, model8 });

        //return Json(new { Response = model1, model2, model3, model4});

    }
}

Ajax I'm get data from the controller and place in variable "response" :

function getData() {
//alert(data["name"]);
$.ajax({
    url: '/Markers/GetMarkers', //Controller/Akcja

    type: 'POST',
    dataType: "json",
    data: JSON.stringify(),
    contentType: 'application/json; charset=utf-8',

    success: function(response) {
        initMap(response);
    },

    error: function(error) {
        alert('error');
    }

in function initMap show Object JSON console.log(data):

function initMap(data) {

console.log(data);

    var map = new google.maps.Map(
    document.getElementById('map'),
    { zoom: 10, center: { lat: 53.42894, lng: 14.55302 } });

// test, do not work
var myMarkers = $.parseJSON(data);

}

How conver JSON to array Javascript or how show add markers on map ?

4
  • 2
    Please post the code in the question itself and not upload screenshots of the code instead. Commented Dec 14, 2018 at 21:06
  • Sorry, I edit post. Commented Dec 14, 2018 at 21:16
  • Possible duplicate of How to convert JSON object to JavaScript array Commented Dec 14, 2018 at 21:24
  • So I searched badly Commented Dec 14, 2018 at 21:33

1 Answer 1

1

This should fix the problem:

//Parse the array
var jsonArrayParsed = JSON.parse(yourJsonArray);

// Empty js array
var jsArray = [];

// Loop json array and add all the items to a regular js array.
for (var item in jsonArray){
  if (jsonArray.hasOwnProperty(item)){
    // add to your js array
    jsArray.push(item);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So I have to create var arrayJs = new Array(); and in loop if add item to arrayJs? if (jsonArray.hasOwnProperty(item)){ arrayJs = item; }
One more thing, my JSON have data for example : Object model1: {title: "Galaxy", lat: 53.43382, lng: 14.55559}and use your help code and I get in array : Array(8) 0: "model1" 1: "model2" 2: "model3" How change JASON to array that values in array will for example how in JSON ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.