0

I'm trying to get my JSON data with a console app using visual studio 2012 and c#. This is my code :

using System.Linq;
using System.Text;
using System.Net;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.Web.Helpers;
using System.IO;
using System.Runtime.Serialization.Json;

namespace JSONTest
{

    class Program
    {
        static void Main(string[] args)
        {
             //create a new instance of Webclient and use DownloadString method from the Webclient class to extract download html
            WebClient client = new WebClient();
            string Json1 = client.DownloadString("http://waelhamda.host22.com/localRobot.php");

            JObject objParserd = JObject.Parse(Json1);

            Console.WriteLine(Json1);
            Console.ReadLine();
        }
    }
}

I got this error message while testing the app :

Additional text encountered after finished reading JSON content: <. Path '', line 3, position 0.

and it's in this line of code : JObject objParserd = JObject.Parse(Json1);

6
  • 1
    post the actual content of the json Commented Feb 4, 2016 at 20:34
  • if you go to that URL in your browser and do view source, you will see the issue Commented Feb 4, 2016 at 20:34
  • 2
    I see at the end of localRobot.php additional information <!-- Hosting24 Analytics Code --> <script type= ... , this information is incorrect for json. Commented Feb 4, 2016 at 20:38
  • 1
    That address you have there does NOT output a json content. It outputs a web page with JSON string in the body. Commented Feb 4, 2016 at 20:39
  • @ Dleh : This is the url of the json Data : waelhamda.host22.com/localRobot.php @ Rhumborl : I haven't seen the issue... Commented Feb 4, 2016 at 20:42

2 Answers 2

3

I'm pretty sure that when you execute your client.DownloadString() you receive:

{"success":1,"message":"Post Available!","posts":[{"ID":"3","Name":"Wael","Code":"0000"}]}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

That's why your parsing fails at line 3.

Edit

If you don't have control over what the URL returns, a dumb workaround would be to take only the first line and parse that in JSON.

var firstLine = Json1.Split(Environment.NewLine).FirstOrDefault();
JObject objParserd = JObject.Parse(firstLine);
Sign up to request clarification or add additional context in comments.

4 Comments

That's true , thank you but how can I fix that ? any idea ?
Do you have control on what is returned by the web page? If not, a dumb workaround would be to only take the first line (ie: Json1.Split(Environment.NewLine)[0]) and parse that in JSON.
The stripping would only work if the file format were to be consistent.
Thank you very match , adding this line have solved the problem : var firstLine = Json1.Split('<').FirstOrDefault();
3

The raw source of the page has the following. This is not true JSON. There should be nothing else in the file, and watch for tabs and white space. Make sure there is nothing else in any JSON pages.

This is what Alex Vazhev is saying.

{"success":1,"message":"Post Available!","posts":[{"ID":"3","Name":"Wael","Code":"0000"}]}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

1 Comment

Do you control the source? The next thing would be to pre-parse the return. Striping out anything that is no 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.