【问题标题】:How to retrive the array value into different array?如何将数组值检索到不同的数组中?
【发布时间】:2013-06-11 11:55:52
【问题描述】:

在我的应用程序中,我使用了 Json。这是我的应用程序中的 JSON 响应

This is my Response:
[
    {
    "response": "Success",
    "errorMsg": "",
    "userId": "1",
    "userCompany": "xxxyy",
    "userName": "sham",
    "userAddress": "chennai",
    "userCity": "xxxxx",
    "userMobile": "xxxx",
    "userEmail": "xxx"
},
{
    "response": "Success",
    "errorMsg": "",
    "productImage": "http://www.iii.jpg",
    "productDescription": "Loaded box on pallets - Bart's package",
    "productCost": "10",
    "productBoxWeight": "10.0"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1.4",
    "transportCountry": "Colombia",
    "transportPort": "Havana"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.7",
    "transportCountry": "Brazil",
    "transportPort": "Santos"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "South Africa",
    "transportPort": "Durban"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "Chili",
    "transportPort": "San Antonio"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "2.7",
    "transportCountry": "Australia",
    "transportPort": "Maersk"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Marocco",
    "transportPort": "Casablanca"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Kuwait",
    "transportPort": "Shuwaikh"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Jordan",
    "transportPort": "Aqaba"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.8",
    "transportCountry": "Saoudi Arabia",
    "transportPort": "Jeddah"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.8",
    "transportCountry": "Malta",
    "transportPort": "Maraxklokk"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "Mexico",
    "transportPort": "Veracruz"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1.2",
    "transportCountry": "Thailand",
    "transportPort": "Bangkok"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Thailand",
    "transportPort": "havana"
}
]

如何检索一个数组中的前两个集合和另一个数组中的其他集合...我是新手,请帮我修复它...

【问题讨论】:

  • 你能否给我正确的信息,你需要从那个回复中得到什么?@IOSdev
  • { "response": "Success", "errorMsg": "", "userId": "1", "userCompany": "xxxyy", "userName": "sham", "userAddress ": "chennai", "userCity": "xxxxx", "userMobile": "xxxx", "userEmail": "xxx" },我需要像这样分开每个集合...
  • 更改您的响应,如键:值方式,然后读取响应并保存在 JsonArray 中。您可以根据密钥管理响应以保存适当的数组。
  • 是否可以更改您的回复?
  • 你的 json 格式不是正确的格式亲爱的..请检查 jsonlint 或 jsonviewer?

标签: objective-c json nsarray


【解决方案1】:

JSON 没有将键或值(包括 JSON 数组中的值)定义为任何顺序。因此,您必须在解析后自己对项目进行排序。

为了解析 JSON,您可以使用 any JSON parser available(我更喜欢 SBJson)。 通常解析器提供将 JSON 转换为 NSDictionary 的可能性,因此您可以轻松处理其内容。

【讨论】:

    【解决方案2】:

    为了解析您的 JSON 并获取 Objective-C 对象,请使用以下代码(其中 data 是您刚刚获得的 JSON):

    NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data 
                                              options:NSJSONReadingMutableContainers 
                                              error:&e];
    

    之后,您将拥有一个包含多个字典 (NSDictionary) 的 NSArray。要获得第一个,请执行以下操作:

    if (!jsonArray) {
      NSLog(@"Error parsing JSON: %@", e);
    } else {
        // get the first dictionary
        NSDictionary *dict = [jsonArray objectAtIndex:0];    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多