如何在android中获取自定义json输出的内容

时间:2015-05-23 16:08:08

标签: android json

我想获取此json输出的内容!但我不知道该怎么办?

我的输出:

{ 
  "7916" : { "GoodMainCode" : "7916",
      "amount" : "0",
      "author" : " ",
      "id" : "1168",
      "isbn" : " ",
      "period_print" : "0",
      "price" : "20625",
      "publisher" : "فقيهي مهر",
      "title" : "زيردستي طلقي CLIP BOARD",
      "translator" : " ",
      "year_of_publish" : "0"
    },
  "7989" : { "GoodMainCode" : "7989",
      "amount" : 61,
      "author" : " ",
      "id" : "16827",
      "isbn" : " ",
      "period_print" : "0",
      "price" : "108025",
      "publisher" : "",
      "title" : "يدك اتود5ميل كوه نورB6",
      "translator" : " ",
      "year_of_publish" : "0"
    },
  "8350" : { "GoodMainCode" : "8350",
      "amount" : "0",
      "author" : " ",
      "id" : "1225",
      "isbn" : " ",
      "period_print" : "0",
      "price" : "3375",
      "publisher" : "",
      "title" : "يدك اتودطراحي2ميلJBN",
      "translator" : " ",
      "year_of_publish" : "0"
    }
}

我知道我应该使用json / gson库,但在我的输出中,791679898350是额外的!

******************* 已更新 ************* **************

假设我有一个这样的数组:

Array
(
    (
        [0] => A
        [1] => 20
    )
    (
        [0] => B
        [1] => 10
    )
    (
        [0] => G
        [1] => 5
    )
    (
        [0] => A
        [1] => 15
    )
)

我想删除重复的值并只汇总一行数组:我想要的:

   Array
    (
        (
            [0] => A
            [1] => 35 // <= sum : 20 + 15
        )    
        (
            [0] => B
            [1] => 10
        )    
        (
            [0] => G
            [1] => 5
        )
    )

我的代码:

while($row = $stmt->fetch()){

    $arr = array(
        'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode']), // <= like A in the example
        'title'=> persian_sql_to_php($row['GoodName']),
        'author'=>persian_sql_to_php($row['moalef']),
        'publisher'=>persian_sql_to_php($row['Nasher']),
        'translator'=>persian_sql_to_php($row['Motarjem']),
        'price'=>persian_sql_to_php($row['SellPrice1']),
        'isbn'=>persian_sql_to_php($row['ISBN']),
        'amount'=>persian_sql_to_php($row['Amount']), // <= if GoodMainCode is same key, I must sum it.
        'year_of_publish'=>persian_sql_to_php($row['SaleChap']),
        'period_print'=>persian_sql_to_php($row['NobateChap'])
    );

    array_push($mjson,$arr);
}

//to remove duplicate values and sum amount key values

foreach($mjson as $v){

    if(!isset($result[$v['GoodMainCode']]))
        $result[$v['GoodMainCode']] = $v;
    else
        $result[$v['GoodMainCode']]['amount'] += $v['amount'];
}

1 个答案:

答案 0 :(得分:1)

自定义Json 没有任何意义。它或 Json 不是Json 。 给定JSON具有下一个结构: enter image description here

我认为没有理由申请GSON,因为JSON非常简单。 以下是从树中逐步提取所有元素的代码:

JSONObject json = new JSONObject(response); // where *response* is your response from server or whatever where did you get this json
Iterator keys = json.keys();

while(keys.hasNext()) {
    // loop to get the dynamic key
    String currentDynamicKey = (String)keys.next(); // this is your number at N position

    // get the value of the dynamic key
    JSONObject numbersValue = json.getJSONObject(currentDynamicKey);

    //retrieve values same way and do something with it
}

如果我理解正确,您需要始终使用不同的数字解析JSON,这样会对您有所帮助。