• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1038
  • Last Modified:

Parsing JSON response

I have an unordered list, with each list item attributed with an id that will contain a location code.  After receiving an ajax response from the server, I am provided with an array of location codes that may or may not match the ones in my unordered list; if they do, I need to add a class of "on" to each list item.

What I need is the js needed to parse the json response and then the logic to add the css class if a location code matches one in my list.

Should be trivial for a js ninja, complicated for a novice like me!
   <li id="a7456">Location 1</li>
   <li id="a7457">Location 2</li>
   <li id="a7458">Location 3</li> 
   <li id="a7459">Location 4</li>
// and if a location code matches
<li id="a7456" class="on">Location 1</li>

Open in new window

  • 4
  • 2
2 Solutions
May not help yo uso much if you are not using .NET, but if not you will have to get a JS class that will deserialize for you...

...either way logic for updating CSS class on unordered list items is sound.
****ASPX CODE WITH UL / CSS / and JavaScript****
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Managing JSON</title>
    <style type="text/css">
    <script type="text/javascript">
		//from body onload
		function getJArray()
			jsonArray.GetJArray(rFunction); //call service to get JSON Serialized Array
		//web service returns value which is collected here and deserialized into an array
		function rFunction(rValue)
			var jArray = new Array();
			jArray = Sys.Serialization.JavaScriptSerializer.deserialize(rValue);
		//check ul for li's that have same text as array, adding css class if so...
		function addCSSClass(jArr)
			var ul = document.getElementById('listOfItems');
					//had to add this to trim(could have done diff) li innerHTML because it was coming with space.
					var chkStr = ul.childNodes[i].innerHTML.substring(0,ul.childNodes[i].innerHTML.lastIndexOf(' '));
					if(chkStr == jArr[j])
					{ ul.childNodes[i].className = 'on'; }
<body onload="getJArray();">
    <form id="form1" runat="server">
		<asp:ScriptManager ID="sm" runat="server">
			<Services><asp:ServiceReference Path="~/svc/jsonArray.asmx" /></Services>
		<ul id="listOfItems">
****WEBSERVICE returning serialized array****
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class jsonArray
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    Public Function GetJArray() As String
        Dim valueList As List(Of String) = New List(Of String)
        Dim json As New JavaScriptSerializer()
        Return json.Serialize(valueList.ToArray)
    End Function
End Class

Open in new window

I guess as I think about it JavaScript can pretty well handle a JSON serialized string. You could replace the following:

jArray = Sys.Serialization.JavaScriptSerializer.deserialize(rValue);

jArray = eval(rValue);
Did you give up on this or figure it out? Is there something about my sample that does not make sense? Maybe I can provide things in a different context if I know that you still need help, and what is confusing you.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

aznprncriticAuthor Commented:
hi ddayx10,

Thanks for your proposed solution.  I actually went with this using jquery:

for (var i in json.location) {
   $('[rel=' + json.location[i] + ']').addClass('on');

   <li rel="a7456">Location 1</li>
   <li rel="a7457">Location 2</li>
   <li rel="a7458">Location 3</li>
   <li rel="a7459">Location 4</li>
aznprncriticAuthor Commented:
Used my own solution
I think you might consider assigning some points. You did ask a question. I did give a proper and working solution. Your solution does not adhere to the criteria you laid out as to working with JSON. I stayed engaged with the thread without any feedback from you except to say a week later that you solved it yourself using different techniques.

You did find another solution, but you also solicited help, wasted or not I think it's important that you acknowledge the effort that was given. If you had engaged and decided to go another route I might have a different perspective, but in this case I don't think you should assign yourself points for this.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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