问题:

我有时使用Visual Studio代码编辑包含注释的JSON文件。 VS Code显示错误消息“JSON中不允许注释”。 最好禁用该错误消息(无需删除注释。)

在此输入图像描述

I sometimes use Visual Studio Code to edit JSON files that include comments.VS Code displays an error saying, “Comments are not permitted in JSON.”It would be nice to disable that error message (without having to remove the comments.)

问题:

我正在使用Web API创建一个Web服务。 我实现了一个简单的类

public class ActivityResult
{
    public String code;
    public int indexValue;
    public int primaryCodeReference;
}

然后我在我的控制器内实现了

[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

但是当我调用API传递POST文件时json:

{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}

我收到以下错误消息:

{
    "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ActivityResult' from content with media type 'text/plain'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": "   in System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}

我究竟做错了什么?

I’m creating a web service using Web API.I implemented a simple classAnd then I have implemented inside my controllerBut when I call the API passing in POST the file json:I obtain the following error message:What am I doing wrong?

问题:

我正在尝试获取新闻网站的供稿。 以为我会使用Google的feed API将feedburner供稿转换为json。 以下网址将以json格式从Feed中返回10个帖子。 http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumi

我用下面的代码来获取上述URL的内容

$.ajax({
  type: "GET",
  dataType: "jsonp",
  url: "http://ajax.googleapis.com/ajax/services/feed/load",
  data: {
    "v": "1.0",
    "num": "10",
    "q": "http://feeds.feedburner.com/mathrubhumi"
  },
  success: function(result) {
    //.....
  }
});

但它不起作用,出现以下错误

XMLHttpRequest无法加载http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ http:// localhost ”。

我该如何解决?

I’m trying to fetch the feed of a news website.Thought I’d use google’s feed API to convert the feedburner feed into json.The following url will return 10 posts from the feed, in json format.http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumiI used the following code to get the contents of above urlbut it’s not working and I’m getting the following errorXMLHttpRequest cannot load http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi .No ‘Access-Control-Allow-Origin’ header is present on the requested resource.Origin ‘ http://localhost ‘ is therefore not allowed access.How do I fix this?

问题:

我正在使用JSON请求调用REST服务,它给出了Http 415“不支持的媒体类型”错误。

请求内容类型设置为(“Content-Type”,“application / json; charset = utf8”)。

如果我在请求中不包含Json对象,它可以正常工作。 我正在为json使用google-gson-2.2.4库。

我尝试使用几个不同的库,但它没有任何区别。

有人可以帮我解决这个问题吗?

这是我的代码:

public static void main(String[] args) throws Exception
{

    JsonObject requestJson = new JsonObject();
    String url = "xxx";

    //method call for generating json

    requestJson = generateJSON();
    URL myurl = new URL(url);
    HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);

    con.setRequestProperty("Content-Type", "application/json; charset=utf8");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestProperty("Method", "POST");
    OutputStream os = con.getOutputStream();
    os.write(requestJson.toString().getBytes("UTF-8"));
    os.close();


    StringBuilder sb = new StringBuilder();  
    int HttpResult =con.getResponseCode();
    if(HttpResult ==HttpURLConnection.HTTP_OK){
    BufferedReader br = new BufferedReader(new   InputStreamReader(con.getInputStream(),"utf-8"));  

        String line = null;
        while ((line = br.readLine()) != null) {  
        sb.append(line + "\n");  
        }
         br.close(); 
         System.out.println(""+sb.toString());  

    }else{
        System.out.println(con.getResponseCode());
        System.out.println(con.getResponseMessage());  
    }  

}
public static JsonObject generateJSON () throws MalformedURLException

{
   String s = "http://www.abc.com";
        s.replaceAll("/", "\\/");
    JsonObject reqparam=new JsonObject();
    reqparam.addProperty("type", "arl");
    reqparam.addProperty("action", "remove");
    reqparam.addProperty("domain", "staging");
    reqparam.addProperty("objects", s);
    return reqparam;

}
}

requestJson.toString的值是:

{“type”:“arl”,“action”:“remove”,“domain”:“staging”,“objects”:“ http://www.abc.com ”}

I am calling REST service with JSON request and it gives Http 415 “Unsupported Media Type” error.Request content type is set to (“Content-Type”, “application/json; charset=utf8”).It works fine if I don’t include Json object in the request.I am using google-gson-2.2.4 library for json.I tried using a couple of different libraries but it made no difference.Can anybody please help me to resolve this?Here is my code:Value of requestJson.toString is :{“type”:”arl”,”action”:”remove”,”domain”:”staging”,”objects”:” http://www.abc.com “}

问题:

我收到此错误:

JSON.parse:意外字符

当我在firebug中运行以下语句时:

JSON.parse({"balance":0,"count":0,"time":1323973673061,"firstname":"howard","userId":5383,"localid":1,"freeExpiration":0,"status":false});

为什么会这样呢? JSON字符串对我来说似乎是正确的,我也使用JSHint对其进行了测试。 在上述情况下,传递的对象是服务器响应,其内容类型设置为application/json

I get this error:JSON.parse: unexpected characterwhen I run this statement in firebug:Why is it so?The JSON string seems correct to me and I also tested it using JSHint.The passed object in the above case is a server response with content type set to application/json