Showing posts with label JSON Object. Show all posts
Showing posts with label JSON Object. Show all posts

Thursday, May 10, 2018

How do you create a JSON object?

You go by definition of a JSON Object. JSON Object components are placed under curly braces as shown here.

{"name1":"value1","name2":"value2","name3":"value3"}

Here is an example:

var JSONObj = {"Fname":"Jay","LName":"Krishna","Hobby":"Blogging"};

JSON object consists of name/value pairs separated by commas.

In the above names are:
Fname, LName and Hobby

The corresponding values are:
Jay, Krishna and Blogging

Here is an HTML file that writes the values of a JSON object to the browser.

You can upload it to your website (perhaps IIS's Localhost). When you browse to it you get this response.



For a more detailed article go here:

Monday, January 29, 2018

How do you find the value of this JSON object --- {"70":27.4"} ?

These are two validated JSON objects:

I am trying to find the value for the second JSON object:
The code shown at the end works and provides a value of 2.85
----------------------
var jobj2={"tuesday":"2.85"};
alert(jobj2.tuesday );//---alerts 2.85

------------------------------------
If I use the exact code that I used for recovering the value for the first JSON object, I end up with an error.
---------------
var obj2={"70":"27.4"};
alert(obj2.70);//--spawns an error
--------------------
The work around which I guessed that would work for the first object provides the value 27.4

var x;
x=70;
var obj={"x":"27.4"};
alert(obj.x);----//alerts 27.4

---------------------------------------

If you want to take a look, just take out the relevant comments and run. There could be a better way of resolving this rather than re-formatting the question.


Sunday, January 28, 2018

How do you store an array in Local Storage?

My previous post showed how to store a single piece of information in Local Storage (Browser Storage). However instead of a single piece of data you want to store an array of items.
Let us say your array has three elements, rose, jasmine and hyacinth and you want to store in an array named 'flowers'.
The following page shows how you may store array and retrieve array elements using JavaScript. Each statement is commented (view image magnified, if necessary).


ArrayStoreLocalStorage.png

Place this page in IIS's wwwroot folder and browse. You can see the elements in the array in LocalStorage.


ArrayStoreLocalStorage1.png

You can see more clearly here in the Microsoft Edge's developer window. Data stored from the previous post is also seen.



ArrayStoreLocalStorage2.png

Saturday, October 15, 2016

How do you correctly create a JSON formatted string?

Although the rules are simple you could run into problems because of the violation of the rules from those described in the RFC 4627.

For example:

{"company":"Hodentek", "phone":"808-722-6785", "city":"Honolulu"}

is correctly JSON formatted and represent a JSON Object. The object is surrounded by curly brackets({ }).

The above is an example of the structured JSON CLR Object type

JSON admits of another structure, an array as shown here:

["Red", "Blue", "Yellow", "Magenta"]


is a correctly JSON formatted array. The array is surrounded by square brackets([ ])

The above is an example of the structured JSON CLR array type

Here is a JSON formatted version of an XML that I had used in a 2007 post to explain fine points of JSON.

{ "wclass":[
{"student": {"name":"Linda Jones", "legacySkill":"Access, VB 5.0"}},
{"student": {"name":"Adam Davidson", "legacySkill":"Cobol, MainFrame"}},
{"student": {"name":"Charles Boyer", "legacySkill":"HTML, XML"}} ]

I recently noticed that I had committed an error although the code in the post worked quite well when parsed using JavaScript. At that time I had not verified whether it was strictly validated and I had used 'inspection' to write out the JSON looking at the XML.

I recently visited JSON formatting in order to answer a question that came up on http://stackoverflow.com/ and I wanted to check my old code. I found this site which is quite useful for such validation.

https://jsonformatter.curiousconcept.com/

On this site you can check your JSON very easily by just entering your JSON data or a URL reference to it. After typing in, just hit the "Process" button and the page gets updated showing the result. If there are errors the site also shows the errors.


Here is my first iteration:

jsontest_00

Here is the result after processing:


jsontest_001

You can go back to where you typed at the top of the web page, without refreshing the page and make necessary corrections and resubmit by hitting the 'Process' button again. In this way you can arrive at the correct formatting.

This is the correction to my old code:

["wclass",
{"student":{"name":"Linda Jones","legacySkill":"Access, VB 5.0"}
},
{  "student":{"name":"Adam Davidson","legacySkill":"Cobol, MainFrame"}
},
{"student":{"name":"Charles Boyer","legacySkill":"HTML, XML"}
}]


Here is the response that the above is properly validated.


jsontest_031.jpg

This is the error (I should have used a comma(,) instead of a colon(:))  I might have had in my old code:


jsonold.png

There is one more check I will do before I am completely satisfied. I will rerun the JavaScript code to see whether some things have changed.


Saturday, July 18, 2015

How do you create JSON data with Visual Studio?

You can create JSON formatted data using a text writer as it is really a text file specially
formatted and saved with the extension .json. JsonTextwriter is derived from System.IO assembly.

You can look at everything (properties, methods, etc.) in Newtonsoft.Json which you added to a
Visual Studio project in a previous post using the Object Browser in Visual Studio IDE (herein Visual Studio2013 Community edition.

Browse for Newtonsoft.Json in My Solution as shown in your Visual Studio IDE after displaying the Object Brower.

NewtonsoftNamespace.png

The first step is to create a C# (could be VB also, but here it is a simple console project)
Project and add reference to Newtonsoft.JSON as described in the previous post.

The following code taken from the Newtonsoft site was slightly altered to make it work in the
Visual Studio 2013 Community edition(free).

The original code is here:

Origcode.png

In order to build and run this code in Visual Studio you need to provide references to the various assemblies you will be including in the code. You will be using the various methods that Newtonsoft.Json provides which you can see in the object browser.

The code instantiates a String Builder to take the text writer's text (name and value) and build the Json formatted object.

In order to use String Builder you need to reference System.IO assembly and the result of running the code will be displayed in the Console as shown:

ConsoleDisplay.png

However if you want to display the result in the Debug Output window in Visual Studio IDE you need to do a couple of things:

1. You should add a reference to System.Diagnostics.Debug assembly.
    This may not be available in the default installation of Visual Studio 2013 but you should browse for it in the Window Phone related assemblies (this was where it was found)

2. In the project properties Application page you should change the output type from the default 'Console Application' to 'Windows Application'.

WindowsApplication.png

3. Add a line of code to display using System.diagnostics.debug.print()
Now build and run the program and you will display result in the debug output window as shown.

debugprint.png

Here is the code that was run to display the above.
----------------
using System;
using System.Collections.Generic;
using System. Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Diagnostics;


namespace JsonWrite

{
class Program
{
static void Main(string[] args)
{
StringBuilder sb=new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter writer = new JsonTextWriter(sw);


writer.Formatting = Formatting.Indented;

writer.WriteStartObject();
writer.WritePropertyName("CPU");
writer.WriteValue("Intel");
writer.WritePropertyName("PSU");
writer.WriteValue("500W");
writer.WritePropertyName("Drives");
writer.WriteStartArray();
writer.WriteValue("DVD read/writer");
writer.WriteComment("(broken)");
writer.WriteValue("500 gigabyte hard drive");
writer.WriteValue("200 gigabype hard drive");
writer.WriteEnd();
writer.WriteEndObject();

Console.WriteLine(sb.ToString());
// Console.WriteLine(sb.ToString());
System.Diagnostics.Debug.Print(sb.ToString());
}

}
}