2014-01-13 47 views
0

我想从一个url读取json到我的C#应用​​程序中。当我运行的应用程序,我不断收到一个错误:从URL中读取JSON时出错

“附加文本遇到结束后读取JSON内容:{路径”,2号线,位置667"

这是从这个URL

我检查了网页和查看源代码,似乎无法找到问题的如何解决这个

的JSON从PHP数组JSON编码,并呼应导出:?

$args = array('post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'Alcopops'); 

    $loop = new WP_Query($args); 

    while ($loop->have_posts()) : $loop->the_post(); 
    global $product; 
echo json_encode($product); 

    endwhile; 


    wp_reset_query(); 
+0

环绕的Json用方括号。 –

+0

你使用过WebClient吗? –

+0

@SirwanAfifi我用'HttpClient client = new HttpClient(); // HttpResponseMessage response = await client.GetAsync(App.DataServiceUrl +“/ products?category =”+(String)navigationParameter);' – Tester

回答

2

该页面不包含有效的json。看看这个:

"product_type":"simple"}{"id":246,"post":

有{

编辑之间没有逗号}和:

问题是与你的PHP,而不是C#。

试试这个:

$args = array('post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'Alcopops'); 
$loop = new WP_Query($args); 
echo json_encode($loop->get_posts()); 
wp_reset_query(); 
+0

好吧,以及我使用json_encode来获取这种格式的php数组。 – Tester

+0

真的!你可以显示代码在哪里构建数组并调用json_encode?在那里有趣的事情。 –

+1

您是否循环遍历数组中的每个元素,并为每个元素调用json_encode? –

0

使用WebClient

var json = new WebClient().DownloadString("http://cbbnideas.com/brydens-website/products/?category=Alcopops"); 
+0

这是一个Windows 8桌面应用程序。我似乎无法识别WebClient – Tester

+0

@add'使用System.Net;' –

+0

已经这样做了。不起作用。 – Tester