9 Replies - 2165 Views - Last Post: 12 May 2016 - 09:20 PM Rate Topic: -----

#1 aheda4321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 16

I can not Parse JSON to string

Posted 12 May 2016 - 08:01 AM

I have problem with my code. I don't know how to fix. I just want to get all title and gig_url from JSON url and write it to my arraylist. It showed error at: var obj = JObject.Parse(jsons);

"Unexpected character encountered while parsing value: . Path '', line 0, position 0."

My code:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

public void getTitle()
{
            ArrayList myTitle = new ArrayList();
            string url = "https://www.fiverr.com/gigs/endless_page_as_json?host=subcategory&type=endless_auto&category_id=3&sub_category_id=154&limit=48&filter=auto&use_single_query=true&page=1&instart_disable_injection=true";
            using (var webClient = new System.Net.WebClient())
            {
                var jsons = webClient.DownloadString(url);
                if (jsons != null)
                {
                    var obj = JObject.Parse(jsons);
                    var title = (string)obj["gigs"]["title"];
                    var gurl = (string)obj["gigs"]["gigs_url"];
                    myNode1.Add(title + ":" + gurl);
                }
                else
                {
                    MessageBox.Show("nothing found");
                }

            }
}



Is This A Good Question/Topic? 0
  • +

Replies To: I can not Parse JSON to string

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: I can not Parse JSON to string

Posted 12 May 2016 - 09:40 AM

Have you actually looked at the JSON string this returns, before the code attempts to parse it? To make sure it's what you are expecting it to be.

That it's complaining about line 0, position 0 would indicate that the JSON string is corrupt from the start. Perhaps there is some garbage in front of the string that needs to be cleared out, or perhaps they are giving you an invalid result. - Maybe they detect unauthorized use of their site, and send something invalid to prevent it. Are you allowed to scrape their site like this?

The condition on line 25 also doesn't seem to add up. According to the doc entry on the DownloadString method, the method always returns a string, or throws exceptions on error. Would that condition ever be true, then?
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2798
  • View blog
  • Posts: 11,019
  • Joined: 03-December 12

Re: I can not Parse JSON to string

Posted 12 May 2016 - 09:41 AM

what value gets shown when debugging after the values are stored in jsons?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: I can not Parse JSON to string

Posted 12 May 2016 - 10:03 AM

Actually, I checked all that out. Turns out, the site is using GZIP compression on their side. You're getting back compressed data and trying to treat it as a string. This was a novel problem, so I went ahead and made an example:

var url = "https://www.fiverr.com/gigs/endless_page_as_json?host=subcategory&type=endless_auto&category_id=3&sub_category_id=154&limit=48&filter=auto&use_single_query=true&page=1&instart_disable_injection=true";
var handler = new HttpClientHandler 
{
	AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
};

using (var client = new HttpClient(handler))
{
	var data = client.GetStringAsync(url).Result;
	dynamic obj = JObject.Parse(data);
	var title = ((string)(obj.gigs[0].title));
	Console.WriteLine(title);
}


Note: this relies on HttpClient instead of WebClient.

Also, you were trying to retrieve data out of your JSON incorrectly, since obj.gigs is actually an array. I've shown more correct usage here.

Quote

The condition on line 25 also doesn't seem to add up. According to the doc entry on the DownloadString method, the method always returns a string, or throws exceptions on error. Would that condition ever be true, then?


This is why I always use string.IsNullOrWhiteSpace(str) instead of str == null, unless I'm explicitly looking for a null value.
Was This Post Helpful? 0
  • +
  • -

#5 aheda4321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 16

Re: I can not Parse JSON to string

Posted 12 May 2016 - 07:44 PM

Thank for your support but i use VS 2010 and .Net 4 so i can not use System.Net.Http. Please reconsisder

This post has been edited by Skydiver: 12 May 2016 - 07:55 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 aheda4321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 16

Re: I can not Parse JSON to string

Posted 12 May 2016 - 07:52 PM

View Postastonecipher, on 12 May 2016 - 09:41 AM, said:

what value gets shown when debugging after the values are stored in jsons?


Raw value on json url:
{
   "gigs":
        [
           {
"title":"draw an Eye Catching Cartoon TShirt Design",
"cached_slug":"draw-an-eye-catching-cartoon-tshirt-design",
"status":"approved",
"title_full":"draw an Eye Catching Cartoon TShirt Design",
"duration":6,
"price":"$5",
"price_i":5,
"rating":10,
"rating_count":944,
"is_featured":true,
"gig_created":"2014-01-08T11:28:20.000-04:00",
"gig_id":2539315,
"gig_url":"/valiantgraphics/draw-an-eye-catching-cartoon-tshirt-design?context=adv.cat_3.subcat_154\u0026context_type=auto\u0026pos=1\u0026funnel=e3e9b4d8-0c4f-488d-bafa-8bc6049b682e",
...........



What I got after downloadString

      }kƕ_aMV4~aUjK[VQ)Vh"	$%Sƍ$
bi>O7<8=6wv]]#yңomFvVw|?`Yc'=vVmwf;^2bV`<ZM٫u<U2M髻䚕3?ڭwSIȫfj#Uln~,̏! 1N"
 `깹Q,1vqZjL/ֿjA_17R?;
..............



I write a class Webclient to pass Gzip.
But till now I can get raw data from that url and parse to json.
So sad :(
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6840
  • View blog
  • Posts: 23,218
  • Joined: 05-May 12

Re: I can not Parse JSON to string

Posted 12 May 2016 - 07:54 PM

Everything else you are using is relatively modern: JSON, .NET 4, VS2010, yet you are using the .NET 1.1 ArrayList. Why?

Also, there is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#8 aheda4321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 16

Re: I can not Parse JSON to string

Posted 12 May 2016 - 07:54 PM

View PostAtli, on 12 May 2016 - 09:40 AM, said:

Have you actually looked at the JSON string this returns, before the code attempts to parse it? To make sure it's what you are expecting it to be.

That it's complaining about line 0, position 0 would indicate that the JSON string is corrupt from the start. Perhaps there is some garbage in front of the string that needs to be cleared out, or perhaps they are giving you an invalid result. - Maybe they detect unauthorized use of their site, and send something invalid to prevent it. Are you allowed to scrape their site like this?

The condition on line 25 also doesn't seem to add up. According to the doc entry on the DownloadString method, the method always returns a string, or throws exceptions on error. Would that condition ever be true, then?


Hi, I tried it and this is what I get

      }kƕ_aM)JmimƲ8[y7[ИEHJ߷q#	$tqFKw<8<7wl37w7^5٬gfey<d2yuQ|S^~^{l7jJ:4esM|fp,^W7dd?zS2/nҫ&y|fi
1nliӏnC; /  r_=3"(`L._OۅZƟB ^n?WLW뭟 ) Ċ+nU)5^?FRB		G 	$P|!02[=
Ffoi43*YE*\ezoi~AfZp
...................



I don't know why, I want to get raw data as show on json url
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6840
  • View blog
  • Posts: 23,218
  • Joined: 05-May 12

Re: I can not Parse JSON to string

Posted 12 May 2016 - 08:02 PM

No, it is not shown by the URL to be decompressed. It is your web browser that is decompressing the data for you.

Anyway, a simple search on Stackoverflow shows various techniques to decompress the data by override the HttpWebRequest object used by WebClient class.
Was This Post Helpful? 0
  • +
  • -

#10 aheda4321   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 16

Re: I can not Parse JSON to string

Posted 12 May 2016 - 09:20 PM

I have solved it. Close :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1