0

我正在解析 Json

这是我的java代码

    if(jsonstr!=null){
    try{
        JSONObject   jsonResponse = new JSONObject(jsonstr);

      /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
      /*******  Returns null otherwise.  *******/
      JSONArray jsonproduct = jsonResponse.optJSONArray("products");

      /*********** Process each JSON Node ************/

      int lengthJsonArr = jsonproduct.length();  
      for (int i = 0; i < lengthJsonArr; i++) {
          JSONObject c = jsonproduct.getJSONObject(i);
          itemcode.add(c.get("Code").toString());
          item.add(c.get("Name").toString());
          quantity.add("");
          category.add("CategoryCode");


      }

错误是

org.json.JSONException: 值 [{"categories":[{......................all json}]

我的 json 就像

[ { 类别{[ "a":"s" "b":"g" }, { "a":"s" "b":"g" } ]},

产品{[ "a":"s" "b":"g" }, { "a":"s" "b":"g" } ]} ]

我的 Json 网络服务网址 [webservice][1]

请帮助我如何解决此问题 提前致谢

4

7 回答 7

1

试试看:

替换productscategories

try{
    JSONObject   jsonResponse = new JSONObject(jsonstr);

  /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
  /*******  Returns null otherwise.  *******/
  JSONArray jsonproduct = jsonResponse.optJSONArray("categories");

  /*********** Process each JSON Node ************/

  int lengthJsonArr = jsonproduct.length();  
  for (int i = 0; i < lengthJsonArr; i++) {
      JSONObject c = jsonproduct.getJSONObject(i);
      itemcode.add(c.get("Code").toString());
      item.add(c.get("Name").toString());
      quantity.add("");
      category.add("CategoryCode");


  }
于 2014-05-21T12:29:31.990 回答
0

您的 JSON 无效。

您可以使用在线工具进行检查:http: //json.parser.online.fr/

JSON 语法的链接:http: //www.w3schools.com/json/json_syntax.asp

编辑: 好的,现在我们有了真正的 JSON,它是正确的。

换成. JSONObject_ JSONArray因为您的 JSON一个Array.

于 2014-05-21T12:19:19.797 回答
0

您的 JSON 无效。

也许你想要这样的事情?

{
    "categories": [
        {
            "a": "s",
            "b": "g"
        },
        {
            "a": "s",
            "b": "g"
        }
    ],
    "product": [
        {
            "a": "s",
            "b": "g"
        },
        {
            "a": "s",
            "b": "g"
        }
    ]
}
于 2014-05-21T12:19:26.087 回答
0

尝试这个,

if(jsonstr!=null){
    try{
        JSONArray   jsonResponse = new JSONArray(jsonstr);

 JSONObject   jsonResponse = jsonResponse.get(0);


      /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
      /*******  Returns null otherwise.  *******/
      JSONArray jsonproduct = jsonResponse.getJSONArray("products");

      /*********** Process each JSON Node ************/

      int lengthJsonArr = jsonproduct.length();  
      for (int i = 0; i < lengthJsonArr; i++) {
          JSONObject c = jsonproduct.getJSONObject(i);
          itemcode.add(c.get("Code").toString());
          item.add(c.get("Name").toString());
          quantity.add("");
          category.add("CategoryCode");


      }
于 2014-05-21T12:21:16.327 回答
0

这是因为您使用的 json 无效。并且您正在执行的解析也无效

这是一个很好的链接,可以从 android json 解析开始

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

于 2014-05-21T12:22:40.467 回答
0

发生这种情况是因为您的 json 是从那开始的,jsonArray然后您为什么jsonObject要这样做

[
    {
        "categories": [
            {
                "Code": "1001",
                "Name": "ROOM LINEN",
                "ShortName": "ROLN",
                "DivisionCode": "10",
                "SequenceNo": "3"

您应该尝试@akash moradiya的代码给定的答案,他指向正确的方式。

于 2014-05-21T12:24:27.237 回答
0

尝试这个..

  JSONArray jsonarray = new JSONArray(jsonstr);

  JSONObject jsonResponse = jsonarray.getJSONObject(1);
  JSONArray jsonproduct = jsonResponse.getJSONArray("products");

  for (int i = 0; i < jsonproduct.length(); i++) {
      JSONObject c = jsonproduct.getJSONObject(i);
      itemcode.add(c.getString("Code"));
      item.add(c.getString("Name"));
      quantity.add("");
      category.add("CategoryCode");
  }
于 2014-05-21T12:26:32.550 回答